ak8975.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  1. /*
  2. * A sensor driver for the magnetometer AK8975.
  3. *
  4. * Magnetic compass sensor driver for monitoring magnetic flux information.
  5. *
  6. * Copyright (c) 2010, NVIDIA Corporation.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/slab.h>
  25. #include <linux/i2c.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/err.h>
  28. #include <linux/mutex.h>
  29. #include <linux/delay.h>
  30. #include <linux/bitops.h>
  31. #include <linux/gpio.h>
  32. #include <linux/of_gpio.h>
  33. #include <linux/acpi.h>
  34. #include <linux/regulator/consumer.h>
  35. #include <linux/pm_runtime.h>
  36. #include <linux/iio/iio.h>
  37. #include <linux/iio/sysfs.h>
  38. #include <linux/iio/buffer.h>
  39. #include <linux/iio/trigger.h>
  40. #include <linux/iio/trigger_consumer.h>
  41. #include <linux/iio/triggered_buffer.h>
  42. #include <linux/iio/magnetometer/ak8975.h>
  43. /*
  44. * Register definitions, as well as various shifts and masks to get at the
  45. * individual fields of the registers.
  46. */
  47. #define AK8975_REG_WIA 0x00
  48. #define AK8975_DEVICE_ID 0x48
  49. #define AK8975_REG_INFO 0x01
  50. #define AK8975_REG_ST1 0x02
  51. #define AK8975_REG_ST1_DRDY_SHIFT 0
  52. #define AK8975_REG_ST1_DRDY_MASK (1 << AK8975_REG_ST1_DRDY_SHIFT)
  53. #define AK8975_REG_HXL 0x03
  54. #define AK8975_REG_HXH 0x04
  55. #define AK8975_REG_HYL 0x05
  56. #define AK8975_REG_HYH 0x06
  57. #define AK8975_REG_HZL 0x07
  58. #define AK8975_REG_HZH 0x08
  59. #define AK8975_REG_ST2 0x09
  60. #define AK8975_REG_ST2_DERR_SHIFT 2
  61. #define AK8975_REG_ST2_DERR_MASK (1 << AK8975_REG_ST2_DERR_SHIFT)
  62. #define AK8975_REG_ST2_HOFL_SHIFT 3
  63. #define AK8975_REG_ST2_HOFL_MASK (1 << AK8975_REG_ST2_HOFL_SHIFT)
  64. #define AK8975_REG_CNTL 0x0A
  65. #define AK8975_REG_CNTL_MODE_SHIFT 0
  66. #define AK8975_REG_CNTL_MODE_MASK (0xF << AK8975_REG_CNTL_MODE_SHIFT)
  67. #define AK8975_REG_CNTL_MODE_POWER_DOWN 0x00
  68. #define AK8975_REG_CNTL_MODE_ONCE 0x01
  69. #define AK8975_REG_CNTL_MODE_SELF_TEST 0x08
  70. #define AK8975_REG_CNTL_MODE_FUSE_ROM 0x0F
  71. #define AK8975_REG_RSVC 0x0B
  72. #define AK8975_REG_ASTC 0x0C
  73. #define AK8975_REG_TS1 0x0D
  74. #define AK8975_REG_TS2 0x0E
  75. #define AK8975_REG_I2CDIS 0x0F
  76. #define AK8975_REG_ASAX 0x10
  77. #define AK8975_REG_ASAY 0x11
  78. #define AK8975_REG_ASAZ 0x12
  79. #define AK8975_MAX_REGS AK8975_REG_ASAZ
  80. /*
  81. * AK09912 Register definitions
  82. */
  83. #define AK09912_REG_WIA1 0x00
  84. #define AK09912_REG_WIA2 0x01
  85. #define AK09912_DEVICE_ID 0x04
  86. #define AK09911_DEVICE_ID 0x05
  87. #define AK09911_REG_INFO1 0x02
  88. #define AK09911_REG_INFO2 0x03
  89. #define AK09912_REG_ST1 0x10
  90. #define AK09912_REG_ST1_DRDY_SHIFT 0
  91. #define AK09912_REG_ST1_DRDY_MASK (1 << AK09912_REG_ST1_DRDY_SHIFT)
  92. #define AK09912_REG_HXL 0x11
  93. #define AK09912_REG_HXH 0x12
  94. #define AK09912_REG_HYL 0x13
  95. #define AK09912_REG_HYH 0x14
  96. #define AK09912_REG_HZL 0x15
  97. #define AK09912_REG_HZH 0x16
  98. #define AK09912_REG_TMPS 0x17
  99. #define AK09912_REG_ST2 0x18
  100. #define AK09912_REG_ST2_HOFL_SHIFT 3
  101. #define AK09912_REG_ST2_HOFL_MASK (1 << AK09912_REG_ST2_HOFL_SHIFT)
  102. #define AK09912_REG_CNTL1 0x30
  103. #define AK09912_REG_CNTL2 0x31
  104. #define AK09912_REG_CNTL_MODE_POWER_DOWN 0x00
  105. #define AK09912_REG_CNTL_MODE_ONCE 0x01
  106. #define AK09912_REG_CNTL_MODE_SELF_TEST 0x10
  107. #define AK09912_REG_CNTL_MODE_FUSE_ROM 0x1F
  108. #define AK09912_REG_CNTL2_MODE_SHIFT 0
  109. #define AK09912_REG_CNTL2_MODE_MASK (0x1F << AK09912_REG_CNTL2_MODE_SHIFT)
  110. #define AK09912_REG_CNTL3 0x32
  111. #define AK09912_REG_TS1 0x33
  112. #define AK09912_REG_TS2 0x34
  113. #define AK09912_REG_TS3 0x35
  114. #define AK09912_REG_I2CDIS 0x36
  115. #define AK09912_REG_TS4 0x37
  116. #define AK09912_REG_ASAX 0x60
  117. #define AK09912_REG_ASAY 0x61
  118. #define AK09912_REG_ASAZ 0x62
  119. #define AK09912_MAX_REGS AK09912_REG_ASAZ
  120. /*
  121. * Miscellaneous values.
  122. */
  123. #define AK8975_MAX_CONVERSION_TIMEOUT 500
  124. #define AK8975_CONVERSION_DONE_POLL_TIME 10
  125. #define AK8975_DATA_READY_TIMEOUT ((100*HZ)/1000)
  126. /*
  127. * Precalculate scale factor (in Gauss units) for each axis and
  128. * store in the device data.
  129. *
  130. * This scale factor is axis-dependent, and is derived from 3 calibration
  131. * factors ASA(x), ASA(y), and ASA(z).
  132. *
  133. * These ASA values are read from the sensor device at start of day, and
  134. * cached in the device context struct.
  135. *
  136. * Adjusting the flux value with the sensitivity adjustment value should be
  137. * done via the following formula:
  138. *
  139. * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 )
  140. * where H is the raw value, ASA is the sensitivity adjustment, and Hadj
  141. * is the resultant adjusted value.
  142. *
  143. * We reduce the formula to:
  144. *
  145. * Hadj = H * (ASA + 128) / 256
  146. *
  147. * H is in the range of -4096 to 4095. The magnetometer has a range of
  148. * +-1229uT. To go from the raw value to uT is:
  149. *
  150. * HuT = H * 1229/4096, or roughly, 3/10.
  151. *
  152. * Since 1uT = 0.01 gauss, our final scale factor becomes:
  153. *
  154. * Hadj = H * ((ASA + 128) / 256) * 3/10 * 1/100
  155. * Hadj = H * ((ASA + 128) * 0.003) / 256
  156. *
  157. * Since ASA doesn't change, we cache the resultant scale factor into the
  158. * device context in ak8975_setup().
  159. *
  160. * Given we use IIO_VAL_INT_PLUS_MICRO bit when displaying the scale, we
  161. * multiply the stored scale value by 1e6.
  162. */
  163. static long ak8975_raw_to_gauss(u16 data)
  164. {
  165. return (((long)data + 128) * 3000) / 256;
  166. }
  167. /*
  168. * For AK8963 and AK09911, same calculation, but the device is less sensitive:
  169. *
  170. * H is in the range of +-8190. The magnetometer has a range of
  171. * +-4912uT. To go from the raw value to uT is:
  172. *
  173. * HuT = H * 4912/8190, or roughly, 6/10, instead of 3/10.
  174. */
  175. static long ak8963_09911_raw_to_gauss(u16 data)
  176. {
  177. return (((long)data + 128) * 6000) / 256;
  178. }
  179. /*
  180. * For AK09912, same calculation, except the device is more sensitive:
  181. *
  182. * H is in the range of -32752 to 32752. The magnetometer has a range of
  183. * +-4912uT. To go from the raw value to uT is:
  184. *
  185. * HuT = H * 4912/32752, or roughly, 3/20, instead of 3/10.
  186. */
  187. static long ak09912_raw_to_gauss(u16 data)
  188. {
  189. return (((long)data + 128) * 1500) / 256;
  190. }
  191. /* Compatible Asahi Kasei Compass parts */
  192. enum asahi_compass_chipset {
  193. AK8975,
  194. AK8963,
  195. AK09911,
  196. AK09912,
  197. AK_MAX_TYPE
  198. };
  199. enum ak_ctrl_reg_addr {
  200. ST1,
  201. ST2,
  202. CNTL,
  203. ASA_BASE,
  204. MAX_REGS,
  205. REGS_END,
  206. };
  207. enum ak_ctrl_reg_mask {
  208. ST1_DRDY,
  209. ST2_HOFL,
  210. ST2_DERR,
  211. CNTL_MODE,
  212. MASK_END,
  213. };
  214. enum ak_ctrl_mode {
  215. POWER_DOWN,
  216. MODE_ONCE,
  217. SELF_TEST,
  218. FUSE_ROM,
  219. MODE_END,
  220. };
  221. struct ak_def {
  222. enum asahi_compass_chipset type;
  223. long (*raw_to_gauss)(u16 data);
  224. u16 range;
  225. u8 ctrl_regs[REGS_END];
  226. u8 ctrl_masks[MASK_END];
  227. u8 ctrl_modes[MODE_END];
  228. u8 data_regs[3];
  229. };
  230. static const struct ak_def ak_def_array[AK_MAX_TYPE] = {
  231. {
  232. .type = AK8975,
  233. .raw_to_gauss = ak8975_raw_to_gauss,
  234. .range = 4096,
  235. .ctrl_regs = {
  236. AK8975_REG_ST1,
  237. AK8975_REG_ST2,
  238. AK8975_REG_CNTL,
  239. AK8975_REG_ASAX,
  240. AK8975_MAX_REGS},
  241. .ctrl_masks = {
  242. AK8975_REG_ST1_DRDY_MASK,
  243. AK8975_REG_ST2_HOFL_MASK,
  244. AK8975_REG_ST2_DERR_MASK,
  245. AK8975_REG_CNTL_MODE_MASK},
  246. .ctrl_modes = {
  247. AK8975_REG_CNTL_MODE_POWER_DOWN,
  248. AK8975_REG_CNTL_MODE_ONCE,
  249. AK8975_REG_CNTL_MODE_SELF_TEST,
  250. AK8975_REG_CNTL_MODE_FUSE_ROM},
  251. .data_regs = {
  252. AK8975_REG_HXL,
  253. AK8975_REG_HYL,
  254. AK8975_REG_HZL},
  255. },
  256. {
  257. .type = AK8963,
  258. .raw_to_gauss = ak8963_09911_raw_to_gauss,
  259. .range = 8190,
  260. .ctrl_regs = {
  261. AK8975_REG_ST1,
  262. AK8975_REG_ST2,
  263. AK8975_REG_CNTL,
  264. AK8975_REG_ASAX,
  265. AK8975_MAX_REGS},
  266. .ctrl_masks = {
  267. AK8975_REG_ST1_DRDY_MASK,
  268. AK8975_REG_ST2_HOFL_MASK,
  269. 0,
  270. AK8975_REG_CNTL_MODE_MASK},
  271. .ctrl_modes = {
  272. AK8975_REG_CNTL_MODE_POWER_DOWN,
  273. AK8975_REG_CNTL_MODE_ONCE,
  274. AK8975_REG_CNTL_MODE_SELF_TEST,
  275. AK8975_REG_CNTL_MODE_FUSE_ROM},
  276. .data_regs = {
  277. AK8975_REG_HXL,
  278. AK8975_REG_HYL,
  279. AK8975_REG_HZL},
  280. },
  281. {
  282. .type = AK09911,
  283. .raw_to_gauss = ak8963_09911_raw_to_gauss,
  284. .range = 8192,
  285. .ctrl_regs = {
  286. AK09912_REG_ST1,
  287. AK09912_REG_ST2,
  288. AK09912_REG_CNTL2,
  289. AK09912_REG_ASAX,
  290. AK09912_MAX_REGS},
  291. .ctrl_masks = {
  292. AK09912_REG_ST1_DRDY_MASK,
  293. AK09912_REG_ST2_HOFL_MASK,
  294. 0,
  295. AK09912_REG_CNTL2_MODE_MASK},
  296. .ctrl_modes = {
  297. AK09912_REG_CNTL_MODE_POWER_DOWN,
  298. AK09912_REG_CNTL_MODE_ONCE,
  299. AK09912_REG_CNTL_MODE_SELF_TEST,
  300. AK09912_REG_CNTL_MODE_FUSE_ROM},
  301. .data_regs = {
  302. AK09912_REG_HXL,
  303. AK09912_REG_HYL,
  304. AK09912_REG_HZL},
  305. },
  306. {
  307. .type = AK09912,
  308. .raw_to_gauss = ak09912_raw_to_gauss,
  309. .range = 32752,
  310. .ctrl_regs = {
  311. AK09912_REG_ST1,
  312. AK09912_REG_ST2,
  313. AK09912_REG_CNTL2,
  314. AK09912_REG_ASAX,
  315. AK09912_MAX_REGS},
  316. .ctrl_masks = {
  317. AK09912_REG_ST1_DRDY_MASK,
  318. AK09912_REG_ST2_HOFL_MASK,
  319. 0,
  320. AK09912_REG_CNTL2_MODE_MASK},
  321. .ctrl_modes = {
  322. AK09912_REG_CNTL_MODE_POWER_DOWN,
  323. AK09912_REG_CNTL_MODE_ONCE,
  324. AK09912_REG_CNTL_MODE_SELF_TEST,
  325. AK09912_REG_CNTL_MODE_FUSE_ROM},
  326. .data_regs = {
  327. AK09912_REG_HXL,
  328. AK09912_REG_HYL,
  329. AK09912_REG_HZL},
  330. }
  331. };
  332. /*
  333. * Per-instance context data for the device.
  334. */
  335. struct ak8975_data {
  336. struct i2c_client *client;
  337. const struct ak_def *def;
  338. struct mutex lock;
  339. u8 asa[3];
  340. long raw_to_gauss[3];
  341. int eoc_gpio;
  342. int eoc_irq;
  343. wait_queue_head_t data_ready_queue;
  344. unsigned long flags;
  345. u8 cntl_cache;
  346. struct iio_mount_matrix orientation;
  347. struct regulator *vdd;
  348. struct regulator *vid;
  349. };
  350. /* Enable attached power regulator if any. */
  351. static int ak8975_power_on(const struct ak8975_data *data)
  352. {
  353. int ret;
  354. ret = regulator_enable(data->vdd);
  355. if (ret) {
  356. dev_warn(&data->client->dev,
  357. "Failed to enable specified Vdd supply\n");
  358. return ret;
  359. }
  360. ret = regulator_enable(data->vid);
  361. if (ret) {
  362. dev_warn(&data->client->dev,
  363. "Failed to enable specified Vid supply\n");
  364. return ret;
  365. }
  366. /*
  367. * According to the datasheet the power supply rise time i 200us
  368. * and the minimum wait time before mode setting is 100us, in
  369. * total 300 us. Add some margin and say minimum 500us here.
  370. */
  371. usleep_range(500, 1000);
  372. return 0;
  373. }
  374. /* Disable attached power regulator if any. */
  375. static void ak8975_power_off(const struct ak8975_data *data)
  376. {
  377. regulator_disable(data->vid);
  378. regulator_disable(data->vdd);
  379. }
  380. /*
  381. * Return 0 if the i2c device is the one we expect.
  382. * return a negative error number otherwise
  383. */
  384. static int ak8975_who_i_am(struct i2c_client *client,
  385. enum asahi_compass_chipset type)
  386. {
  387. u8 wia_val[2];
  388. int ret;
  389. /*
  390. * Signature for each device:
  391. * Device | WIA1 | WIA2
  392. * AK09912 | DEVICE_ID | AK09912_DEVICE_ID
  393. * AK09911 | DEVICE_ID | AK09911_DEVICE_ID
  394. * AK8975 | DEVICE_ID | NA
  395. * AK8963 | DEVICE_ID | NA
  396. */
  397. ret = i2c_smbus_read_i2c_block_data_or_emulated(
  398. client, AK09912_REG_WIA1, 2, wia_val);
  399. if (ret < 0) {
  400. dev_err(&client->dev, "Error reading WIA\n");
  401. return ret;
  402. }
  403. if (wia_val[0] != AK8975_DEVICE_ID)
  404. return -ENODEV;
  405. switch (type) {
  406. case AK8975:
  407. case AK8963:
  408. return 0;
  409. case AK09911:
  410. if (wia_val[1] == AK09911_DEVICE_ID)
  411. return 0;
  412. break;
  413. case AK09912:
  414. if (wia_val[1] == AK09912_DEVICE_ID)
  415. return 0;
  416. break;
  417. default:
  418. dev_err(&client->dev, "Type %d unknown\n", type);
  419. }
  420. return -ENODEV;
  421. }
  422. /*
  423. * Helper function to write to CNTL register.
  424. */
  425. static int ak8975_set_mode(struct ak8975_data *data, enum ak_ctrl_mode mode)
  426. {
  427. u8 regval;
  428. int ret;
  429. regval = (data->cntl_cache & ~data->def->ctrl_masks[CNTL_MODE]) |
  430. data->def->ctrl_modes[mode];
  431. ret = i2c_smbus_write_byte_data(data->client,
  432. data->def->ctrl_regs[CNTL], regval);
  433. if (ret < 0) {
  434. return ret;
  435. }
  436. data->cntl_cache = regval;
  437. /* After mode change wait atleast 100us */
  438. usleep_range(100, 500);
  439. return 0;
  440. }
  441. /*
  442. * Handle data ready irq
  443. */
  444. static irqreturn_t ak8975_irq_handler(int irq, void *data)
  445. {
  446. struct ak8975_data *ak8975 = data;
  447. set_bit(0, &ak8975->flags);
  448. wake_up(&ak8975->data_ready_queue);
  449. return IRQ_HANDLED;
  450. }
  451. /*
  452. * Install data ready interrupt handler
  453. */
  454. static int ak8975_setup_irq(struct ak8975_data *data)
  455. {
  456. struct i2c_client *client = data->client;
  457. int rc;
  458. int irq;
  459. init_waitqueue_head(&data->data_ready_queue);
  460. clear_bit(0, &data->flags);
  461. if (client->irq)
  462. irq = client->irq;
  463. else
  464. irq = gpio_to_irq(data->eoc_gpio);
  465. rc = devm_request_irq(&client->dev, irq, ak8975_irq_handler,
  466. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  467. dev_name(&client->dev), data);
  468. if (rc < 0) {
  469. dev_err(&client->dev,
  470. "irq %d request failed, (gpio %d): %d\n",
  471. irq, data->eoc_gpio, rc);
  472. return rc;
  473. }
  474. data->eoc_irq = irq;
  475. return rc;
  476. }
  477. /*
  478. * Perform some start-of-day setup, including reading the asa calibration
  479. * values and caching them.
  480. */
  481. static int ak8975_setup(struct i2c_client *client)
  482. {
  483. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  484. struct ak8975_data *data = iio_priv(indio_dev);
  485. int ret;
  486. /* Write the fused rom access mode. */
  487. ret = ak8975_set_mode(data, FUSE_ROM);
  488. if (ret < 0) {
  489. dev_err(&client->dev, "Error in setting fuse access mode\n");
  490. return ret;
  491. }
  492. /* Get asa data and store in the device data. */
  493. ret = i2c_smbus_read_i2c_block_data_or_emulated(
  494. client, data->def->ctrl_regs[ASA_BASE],
  495. 3, data->asa);
  496. if (ret < 0) {
  497. dev_err(&client->dev, "Not able to read asa data\n");
  498. return ret;
  499. }
  500. /* After reading fuse ROM data set power-down mode */
  501. ret = ak8975_set_mode(data, POWER_DOWN);
  502. if (ret < 0) {
  503. dev_err(&client->dev, "Error in setting power-down mode\n");
  504. return ret;
  505. }
  506. if (data->eoc_gpio > 0 || client->irq > 0) {
  507. ret = ak8975_setup_irq(data);
  508. if (ret < 0) {
  509. dev_err(&client->dev,
  510. "Error setting data ready interrupt\n");
  511. return ret;
  512. }
  513. }
  514. data->raw_to_gauss[0] = data->def->raw_to_gauss(data->asa[0]);
  515. data->raw_to_gauss[1] = data->def->raw_to_gauss(data->asa[1]);
  516. data->raw_to_gauss[2] = data->def->raw_to_gauss(data->asa[2]);
  517. return 0;
  518. }
  519. static int wait_conversion_complete_gpio(struct ak8975_data *data)
  520. {
  521. struct i2c_client *client = data->client;
  522. u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
  523. int ret;
  524. /* Wait for the conversion to complete. */
  525. while (timeout_ms) {
  526. msleep(AK8975_CONVERSION_DONE_POLL_TIME);
  527. if (gpio_get_value(data->eoc_gpio))
  528. break;
  529. timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
  530. }
  531. if (!timeout_ms) {
  532. dev_err(&client->dev, "Conversion timeout happened\n");
  533. return -EINVAL;
  534. }
  535. ret = i2c_smbus_read_byte_data(client, data->def->ctrl_regs[ST1]);
  536. if (ret < 0)
  537. dev_err(&client->dev, "Error in reading ST1\n");
  538. return ret;
  539. }
  540. static int wait_conversion_complete_polled(struct ak8975_data *data)
  541. {
  542. struct i2c_client *client = data->client;
  543. u8 read_status;
  544. u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
  545. int ret;
  546. /* Wait for the conversion to complete. */
  547. while (timeout_ms) {
  548. msleep(AK8975_CONVERSION_DONE_POLL_TIME);
  549. ret = i2c_smbus_read_byte_data(client,
  550. data->def->ctrl_regs[ST1]);
  551. if (ret < 0) {
  552. dev_err(&client->dev, "Error in reading ST1\n");
  553. return ret;
  554. }
  555. read_status = ret;
  556. if (read_status)
  557. break;
  558. timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
  559. }
  560. if (!timeout_ms) {
  561. dev_err(&client->dev, "Conversion timeout happened\n");
  562. return -EINVAL;
  563. }
  564. return read_status;
  565. }
  566. /* Returns 0 if the end of conversion interrupt occured or -ETIME otherwise */
  567. static int wait_conversion_complete_interrupt(struct ak8975_data *data)
  568. {
  569. int ret;
  570. ret = wait_event_timeout(data->data_ready_queue,
  571. test_bit(0, &data->flags),
  572. AK8975_DATA_READY_TIMEOUT);
  573. clear_bit(0, &data->flags);
  574. return ret > 0 ? 0 : -ETIME;
  575. }
  576. static int ak8975_start_read_axis(struct ak8975_data *data,
  577. const struct i2c_client *client)
  578. {
  579. /* Set up the device for taking a sample. */
  580. int ret = ak8975_set_mode(data, MODE_ONCE);
  581. if (ret < 0) {
  582. dev_err(&client->dev, "Error in setting operating mode\n");
  583. return ret;
  584. }
  585. /* Wait for the conversion to complete. */
  586. if (data->eoc_irq)
  587. ret = wait_conversion_complete_interrupt(data);
  588. else if (gpio_is_valid(data->eoc_gpio))
  589. ret = wait_conversion_complete_gpio(data);
  590. else
  591. ret = wait_conversion_complete_polled(data);
  592. if (ret < 0)
  593. return ret;
  594. /* This will be executed only for non-interrupt based waiting case */
  595. if (ret & data->def->ctrl_masks[ST1_DRDY]) {
  596. ret = i2c_smbus_read_byte_data(client,
  597. data->def->ctrl_regs[ST2]);
  598. if (ret < 0) {
  599. dev_err(&client->dev, "Error in reading ST2\n");
  600. return ret;
  601. }
  602. if (ret & (data->def->ctrl_masks[ST2_DERR] |
  603. data->def->ctrl_masks[ST2_HOFL])) {
  604. dev_err(&client->dev, "ST2 status error 0x%x\n", ret);
  605. return -EINVAL;
  606. }
  607. }
  608. return 0;
  609. }
  610. /* Retrieve raw flux value for one of the x, y, or z axis. */
  611. static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
  612. {
  613. struct ak8975_data *data = iio_priv(indio_dev);
  614. const struct i2c_client *client = data->client;
  615. const struct ak_def *def = data->def;
  616. __le16 rval;
  617. u16 buff;
  618. int ret;
  619. pm_runtime_get_sync(&data->client->dev);
  620. mutex_lock(&data->lock);
  621. ret = ak8975_start_read_axis(data, client);
  622. if (ret)
  623. goto exit;
  624. ret = i2c_smbus_read_i2c_block_data_or_emulated(
  625. client, def->data_regs[index],
  626. sizeof(rval), (u8*)&rval);
  627. if (ret < 0)
  628. goto exit;
  629. mutex_unlock(&data->lock);
  630. pm_runtime_mark_last_busy(&data->client->dev);
  631. pm_runtime_put_autosuspend(&data->client->dev);
  632. /* Swap bytes and convert to valid range. */
  633. buff = le16_to_cpu(rval);
  634. *val = clamp_t(s16, buff, -def->range, def->range);
  635. return IIO_VAL_INT;
  636. exit:
  637. mutex_unlock(&data->lock);
  638. dev_err(&client->dev, "Error in reading axis\n");
  639. return ret;
  640. }
  641. static int ak8975_read_raw(struct iio_dev *indio_dev,
  642. struct iio_chan_spec const *chan,
  643. int *val, int *val2,
  644. long mask)
  645. {
  646. struct ak8975_data *data = iio_priv(indio_dev);
  647. switch (mask) {
  648. case IIO_CHAN_INFO_RAW:
  649. return ak8975_read_axis(indio_dev, chan->address, val);
  650. case IIO_CHAN_INFO_SCALE:
  651. *val = 0;
  652. *val2 = data->raw_to_gauss[chan->address];
  653. return IIO_VAL_INT_PLUS_MICRO;
  654. }
  655. return -EINVAL;
  656. }
  657. static const struct iio_mount_matrix *
  658. ak8975_get_mount_matrix(const struct iio_dev *indio_dev,
  659. const struct iio_chan_spec *chan)
  660. {
  661. return &((struct ak8975_data *)iio_priv(indio_dev))->orientation;
  662. }
  663. static const struct iio_chan_spec_ext_info ak8975_ext_info[] = {
  664. IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, ak8975_get_mount_matrix),
  665. { },
  666. };
  667. #define AK8975_CHANNEL(axis, index) \
  668. { \
  669. .type = IIO_MAGN, \
  670. .modified = 1, \
  671. .channel2 = IIO_MOD_##axis, \
  672. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  673. BIT(IIO_CHAN_INFO_SCALE), \
  674. .address = index, \
  675. .scan_index = index, \
  676. .scan_type = { \
  677. .sign = 's', \
  678. .realbits = 16, \
  679. .storagebits = 16, \
  680. .endianness = IIO_CPU \
  681. }, \
  682. .ext_info = ak8975_ext_info, \
  683. }
  684. static const struct iio_chan_spec ak8975_channels[] = {
  685. AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2),
  686. IIO_CHAN_SOFT_TIMESTAMP(3),
  687. };
  688. static const unsigned long ak8975_scan_masks[] = { 0x7, 0 };
  689. static const struct iio_info ak8975_info = {
  690. .read_raw = &ak8975_read_raw,
  691. };
  692. #ifdef CONFIG_ACPI
  693. static const struct acpi_device_id ak_acpi_match[] = {
  694. {"AK8975", AK8975},
  695. {"AK8963", AK8963},
  696. {"INVN6500", AK8963},
  697. {"AK009911", AK09911},
  698. {"AK09911", AK09911},
  699. {"AK09912", AK09912},
  700. { },
  701. };
  702. MODULE_DEVICE_TABLE(acpi, ak_acpi_match);
  703. #endif
  704. static const char *ak8975_match_acpi_device(struct device *dev,
  705. enum asahi_compass_chipset *chipset)
  706. {
  707. const struct acpi_device_id *id;
  708. id = acpi_match_device(dev->driver->acpi_match_table, dev);
  709. if (!id)
  710. return NULL;
  711. *chipset = (int)id->driver_data;
  712. return dev_name(dev);
  713. }
  714. static void ak8975_fill_buffer(struct iio_dev *indio_dev)
  715. {
  716. struct ak8975_data *data = iio_priv(indio_dev);
  717. const struct i2c_client *client = data->client;
  718. const struct ak_def *def = data->def;
  719. int ret;
  720. s16 buff[8]; /* 3 x 16 bits axis values + 1 aligned 64 bits timestamp */
  721. __le16 fval[3];
  722. mutex_lock(&data->lock);
  723. ret = ak8975_start_read_axis(data, client);
  724. if (ret)
  725. goto unlock;
  726. /*
  727. * For each axis, read the flux value from the appropriate register
  728. * (the register is specified in the iio device attributes).
  729. */
  730. ret = i2c_smbus_read_i2c_block_data_or_emulated(client,
  731. def->data_regs[0],
  732. 3 * sizeof(fval[0]),
  733. (u8 *)fval);
  734. if (ret < 0)
  735. goto unlock;
  736. mutex_unlock(&data->lock);
  737. /* Clamp to valid range. */
  738. buff[0] = clamp_t(s16, le16_to_cpu(fval[0]), -def->range, def->range);
  739. buff[1] = clamp_t(s16, le16_to_cpu(fval[1]), -def->range, def->range);
  740. buff[2] = clamp_t(s16, le16_to_cpu(fval[2]), -def->range, def->range);
  741. iio_push_to_buffers_with_timestamp(indio_dev, buff,
  742. iio_get_time_ns(indio_dev));
  743. return;
  744. unlock:
  745. mutex_unlock(&data->lock);
  746. dev_err(&client->dev, "Error in reading axes block\n");
  747. }
  748. static irqreturn_t ak8975_handle_trigger(int irq, void *p)
  749. {
  750. const struct iio_poll_func *pf = p;
  751. struct iio_dev *indio_dev = pf->indio_dev;
  752. ak8975_fill_buffer(indio_dev);
  753. iio_trigger_notify_done(indio_dev->trig);
  754. return IRQ_HANDLED;
  755. }
  756. static int ak8975_probe(struct i2c_client *client,
  757. const struct i2c_device_id *id)
  758. {
  759. struct ak8975_data *data;
  760. struct iio_dev *indio_dev;
  761. int eoc_gpio;
  762. int err;
  763. const char *name = NULL;
  764. enum asahi_compass_chipset chipset = AK_MAX_TYPE;
  765. const struct ak8975_platform_data *pdata =
  766. dev_get_platdata(&client->dev);
  767. /* Grab and set up the supplied GPIO. */
  768. if (pdata)
  769. eoc_gpio = pdata->eoc_gpio;
  770. else if (client->dev.of_node)
  771. eoc_gpio = of_get_gpio(client->dev.of_node, 0);
  772. else
  773. eoc_gpio = -1;
  774. if (eoc_gpio == -EPROBE_DEFER)
  775. return -EPROBE_DEFER;
  776. /* We may not have a GPIO based IRQ to scan, that is fine, we will
  777. poll if so */
  778. if (gpio_is_valid(eoc_gpio)) {
  779. err = devm_gpio_request_one(&client->dev, eoc_gpio,
  780. GPIOF_IN, "ak_8975");
  781. if (err < 0) {
  782. dev_err(&client->dev,
  783. "failed to request GPIO %d, error %d\n",
  784. eoc_gpio, err);
  785. return err;
  786. }
  787. }
  788. /* Register with IIO */
  789. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  790. if (indio_dev == NULL)
  791. return -ENOMEM;
  792. data = iio_priv(indio_dev);
  793. i2c_set_clientdata(client, indio_dev);
  794. data->client = client;
  795. data->eoc_gpio = eoc_gpio;
  796. data->eoc_irq = 0;
  797. if (!pdata) {
  798. err = of_iio_read_mount_matrix(&client->dev,
  799. "mount-matrix",
  800. &data->orientation);
  801. if (err)
  802. return err;
  803. } else
  804. data->orientation = pdata->orientation;
  805. /* id will be NULL when enumerated via ACPI */
  806. if (id) {
  807. chipset = (enum asahi_compass_chipset)(id->driver_data);
  808. name = id->name;
  809. } else if (ACPI_HANDLE(&client->dev)) {
  810. name = ak8975_match_acpi_device(&client->dev, &chipset);
  811. if (!name)
  812. return -ENODEV;
  813. } else
  814. return -ENOSYS;
  815. if (chipset >= AK_MAX_TYPE) {
  816. dev_err(&client->dev, "AKM device type unsupported: %d\n",
  817. chipset);
  818. return -ENODEV;
  819. }
  820. data->def = &ak_def_array[chipset];
  821. /* Fetch the regulators */
  822. data->vdd = devm_regulator_get(&client->dev, "vdd");
  823. if (IS_ERR(data->vdd))
  824. return PTR_ERR(data->vdd);
  825. data->vid = devm_regulator_get(&client->dev, "vid");
  826. if (IS_ERR(data->vid))
  827. return PTR_ERR(data->vid);
  828. err = ak8975_power_on(data);
  829. if (err)
  830. return err;
  831. err = ak8975_who_i_am(client, data->def->type);
  832. if (err < 0) {
  833. dev_err(&client->dev, "Unexpected device\n");
  834. goto power_off;
  835. }
  836. dev_dbg(&client->dev, "Asahi compass chip %s\n", name);
  837. /* Perform some basic start-of-day setup of the device. */
  838. err = ak8975_setup(client);
  839. if (err < 0) {
  840. dev_err(&client->dev, "%s initialization fails\n", name);
  841. goto power_off;
  842. }
  843. mutex_init(&data->lock);
  844. indio_dev->dev.parent = &client->dev;
  845. indio_dev->channels = ak8975_channels;
  846. indio_dev->num_channels = ARRAY_SIZE(ak8975_channels);
  847. indio_dev->info = &ak8975_info;
  848. indio_dev->available_scan_masks = ak8975_scan_masks;
  849. indio_dev->modes = INDIO_DIRECT_MODE;
  850. indio_dev->name = name;
  851. err = iio_triggered_buffer_setup(indio_dev, NULL, ak8975_handle_trigger,
  852. NULL);
  853. if (err) {
  854. dev_err(&client->dev, "triggered buffer setup failed\n");
  855. goto power_off;
  856. }
  857. err = iio_device_register(indio_dev);
  858. if (err) {
  859. dev_err(&client->dev, "device register failed\n");
  860. goto cleanup_buffer;
  861. }
  862. /* Enable runtime PM */
  863. pm_runtime_get_noresume(&client->dev);
  864. pm_runtime_set_active(&client->dev);
  865. pm_runtime_enable(&client->dev);
  866. /*
  867. * The device comes online in 500us, so add two orders of magnitude
  868. * of delay before autosuspending: 50 ms.
  869. */
  870. pm_runtime_set_autosuspend_delay(&client->dev, 50);
  871. pm_runtime_use_autosuspend(&client->dev);
  872. pm_runtime_put(&client->dev);
  873. return 0;
  874. cleanup_buffer:
  875. iio_triggered_buffer_cleanup(indio_dev);
  876. power_off:
  877. ak8975_power_off(data);
  878. return err;
  879. }
  880. static int ak8975_remove(struct i2c_client *client)
  881. {
  882. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  883. struct ak8975_data *data = iio_priv(indio_dev);
  884. pm_runtime_get_sync(&client->dev);
  885. pm_runtime_put_noidle(&client->dev);
  886. pm_runtime_disable(&client->dev);
  887. iio_device_unregister(indio_dev);
  888. iio_triggered_buffer_cleanup(indio_dev);
  889. ak8975_set_mode(data, POWER_DOWN);
  890. ak8975_power_off(data);
  891. return 0;
  892. }
  893. #ifdef CONFIG_PM
  894. static int ak8975_runtime_suspend(struct device *dev)
  895. {
  896. struct i2c_client *client = to_i2c_client(dev);
  897. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  898. struct ak8975_data *data = iio_priv(indio_dev);
  899. int ret;
  900. /* Set the device in power down if it wasn't already */
  901. ret = ak8975_set_mode(data, POWER_DOWN);
  902. if (ret < 0) {
  903. dev_err(&client->dev, "Error in setting power-down mode\n");
  904. return ret;
  905. }
  906. /* Next cut the regulators */
  907. ak8975_power_off(data);
  908. return 0;
  909. }
  910. static int ak8975_runtime_resume(struct device *dev)
  911. {
  912. struct i2c_client *client = to_i2c_client(dev);
  913. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  914. struct ak8975_data *data = iio_priv(indio_dev);
  915. int ret;
  916. /* Take up the regulators */
  917. ak8975_power_on(data);
  918. /*
  919. * We come up in powered down mode, the reading routines will
  920. * put us in the mode to read values later.
  921. */
  922. ret = ak8975_set_mode(data, POWER_DOWN);
  923. if (ret < 0) {
  924. dev_err(&client->dev, "Error in setting power-down mode\n");
  925. return ret;
  926. }
  927. return 0;
  928. }
  929. #endif /* CONFIG_PM */
  930. static const struct dev_pm_ops ak8975_dev_pm_ops = {
  931. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  932. pm_runtime_force_resume)
  933. SET_RUNTIME_PM_OPS(ak8975_runtime_suspend,
  934. ak8975_runtime_resume, NULL)
  935. };
  936. static const struct i2c_device_id ak8975_id[] = {
  937. {"ak8975", AK8975},
  938. {"ak8963", AK8963},
  939. {"AK8963", AK8963},
  940. {"ak09911", AK09911},
  941. {"ak09912", AK09912},
  942. {}
  943. };
  944. MODULE_DEVICE_TABLE(i2c, ak8975_id);
  945. static const struct of_device_id ak8975_of_match[] = {
  946. { .compatible = "asahi-kasei,ak8975", },
  947. { .compatible = "ak8975", },
  948. { .compatible = "asahi-kasei,ak8963", },
  949. { .compatible = "ak8963", },
  950. { .compatible = "asahi-kasei,ak09911", },
  951. { .compatible = "ak09911", },
  952. { .compatible = "asahi-kasei,ak09912", },
  953. { .compatible = "ak09912", },
  954. {}
  955. };
  956. MODULE_DEVICE_TABLE(of, ak8975_of_match);
  957. static struct i2c_driver ak8975_driver = {
  958. .driver = {
  959. .name = "ak8975",
  960. .pm = &ak8975_dev_pm_ops,
  961. .of_match_table = of_match_ptr(ak8975_of_match),
  962. .acpi_match_table = ACPI_PTR(ak_acpi_match),
  963. },
  964. .probe = ak8975_probe,
  965. .remove = ak8975_remove,
  966. .id_table = ak8975_id,
  967. };
  968. module_i2c_driver(ak8975_driver);
  969. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  970. MODULE_DESCRIPTION("AK8975 magnetometer driver");
  971. MODULE_LICENSE("GPL");