ak8975.c 27 KB

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