ak8975.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  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/iio/iio.h>
  35. #include <linux/iio/sysfs.h>
  36. /*
  37. * Register definitions, as well as various shifts and masks to get at the
  38. * individual fields of the registers.
  39. */
  40. #define AK8975_REG_WIA 0x00
  41. #define AK8975_DEVICE_ID 0x48
  42. #define AK8975_REG_INFO 0x01
  43. #define AK8975_REG_ST1 0x02
  44. #define AK8975_REG_ST1_DRDY_SHIFT 0
  45. #define AK8975_REG_ST1_DRDY_MASK (1 << AK8975_REG_ST1_DRDY_SHIFT)
  46. #define AK8975_REG_HXL 0x03
  47. #define AK8975_REG_HXH 0x04
  48. #define AK8975_REG_HYL 0x05
  49. #define AK8975_REG_HYH 0x06
  50. #define AK8975_REG_HZL 0x07
  51. #define AK8975_REG_HZH 0x08
  52. #define AK8975_REG_ST2 0x09
  53. #define AK8975_REG_ST2_DERR_SHIFT 2
  54. #define AK8975_REG_ST2_DERR_MASK (1 << AK8975_REG_ST2_DERR_SHIFT)
  55. #define AK8975_REG_ST2_HOFL_SHIFT 3
  56. #define AK8975_REG_ST2_HOFL_MASK (1 << AK8975_REG_ST2_HOFL_SHIFT)
  57. #define AK8975_REG_CNTL 0x0A
  58. #define AK8975_REG_CNTL_MODE_SHIFT 0
  59. #define AK8975_REG_CNTL_MODE_MASK (0xF << AK8975_REG_CNTL_MODE_SHIFT)
  60. #define AK8975_REG_CNTL_MODE_POWER_DOWN 0x00
  61. #define AK8975_REG_CNTL_MODE_ONCE 0x01
  62. #define AK8975_REG_CNTL_MODE_SELF_TEST 0x08
  63. #define AK8975_REG_CNTL_MODE_FUSE_ROM 0x0F
  64. #define AK8975_REG_RSVC 0x0B
  65. #define AK8975_REG_ASTC 0x0C
  66. #define AK8975_REG_TS1 0x0D
  67. #define AK8975_REG_TS2 0x0E
  68. #define AK8975_REG_I2CDIS 0x0F
  69. #define AK8975_REG_ASAX 0x10
  70. #define AK8975_REG_ASAY 0x11
  71. #define AK8975_REG_ASAZ 0x12
  72. #define AK8975_MAX_REGS AK8975_REG_ASAZ
  73. /*
  74. * AK09912 Register definitions
  75. */
  76. #define AK09912_REG_WIA1 0x00
  77. #define AK09912_REG_WIA2 0x01
  78. #define AK09912_DEVICE_ID 0x04
  79. #define AK09911_DEVICE_ID 0x05
  80. #define AK09911_REG_INFO1 0x02
  81. #define AK09911_REG_INFO2 0x03
  82. #define AK09912_REG_ST1 0x10
  83. #define AK09912_REG_ST1_DRDY_SHIFT 0
  84. #define AK09912_REG_ST1_DRDY_MASK (1 << AK09912_REG_ST1_DRDY_SHIFT)
  85. #define AK09912_REG_HXL 0x11
  86. #define AK09912_REG_HXH 0x12
  87. #define AK09912_REG_HYL 0x13
  88. #define AK09912_REG_HYH 0x14
  89. #define AK09912_REG_HZL 0x15
  90. #define AK09912_REG_HZH 0x16
  91. #define AK09912_REG_TMPS 0x17
  92. #define AK09912_REG_ST2 0x18
  93. #define AK09912_REG_ST2_HOFL_SHIFT 3
  94. #define AK09912_REG_ST2_HOFL_MASK (1 << AK09912_REG_ST2_HOFL_SHIFT)
  95. #define AK09912_REG_CNTL1 0x30
  96. #define AK09912_REG_CNTL2 0x31
  97. #define AK09912_REG_CNTL_MODE_POWER_DOWN 0x00
  98. #define AK09912_REG_CNTL_MODE_ONCE 0x01
  99. #define AK09912_REG_CNTL_MODE_SELF_TEST 0x10
  100. #define AK09912_REG_CNTL_MODE_FUSE_ROM 0x1F
  101. #define AK09912_REG_CNTL2_MODE_SHIFT 0
  102. #define AK09912_REG_CNTL2_MODE_MASK (0x1F << AK09912_REG_CNTL2_MODE_SHIFT)
  103. #define AK09912_REG_CNTL3 0x32
  104. #define AK09912_REG_TS1 0x33
  105. #define AK09912_REG_TS2 0x34
  106. #define AK09912_REG_TS3 0x35
  107. #define AK09912_REG_I2CDIS 0x36
  108. #define AK09912_REG_TS4 0x37
  109. #define AK09912_REG_ASAX 0x60
  110. #define AK09912_REG_ASAY 0x61
  111. #define AK09912_REG_ASAZ 0x62
  112. #define AK09912_MAX_REGS AK09912_REG_ASAZ
  113. /*
  114. * Miscellaneous values.
  115. */
  116. #define AK8975_MAX_CONVERSION_TIMEOUT 500
  117. #define AK8975_CONVERSION_DONE_POLL_TIME 10
  118. #define AK8975_DATA_READY_TIMEOUT ((100*HZ)/1000)
  119. /*
  120. * Precalculate scale factor (in Gauss units) for each axis and
  121. * store in the device data.
  122. *
  123. * This scale factor is axis-dependent, and is derived from 3 calibration
  124. * factors ASA(x), ASA(y), and ASA(z).
  125. *
  126. * These ASA values are read from the sensor device at start of day, and
  127. * cached in the device context struct.
  128. *
  129. * Adjusting the flux value with the sensitivity adjustment value should be
  130. * done via the following formula:
  131. *
  132. * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 )
  133. * where H is the raw value, ASA is the sensitivity adjustment, and Hadj
  134. * is the resultant adjusted value.
  135. *
  136. * We reduce the formula to:
  137. *
  138. * Hadj = H * (ASA + 128) / 256
  139. *
  140. * H is in the range of -4096 to 4095. The magnetometer has a range of
  141. * +-1229uT. To go from the raw value to uT is:
  142. *
  143. * HuT = H * 1229/4096, or roughly, 3/10.
  144. *
  145. * Since 1uT = 0.01 gauss, our final scale factor becomes:
  146. *
  147. * Hadj = H * ((ASA + 128) / 256) * 3/10 * 1/100
  148. * Hadj = H * ((ASA + 128) * 0.003) / 256
  149. *
  150. * Since ASA doesn't change, we cache the resultant scale factor into the
  151. * device context in ak8975_setup().
  152. *
  153. * Given we use IIO_VAL_INT_PLUS_MICRO bit when displaying the scale, we
  154. * multiply the stored scale value by 1e6.
  155. */
  156. static long ak8975_raw_to_gauss(u16 data)
  157. {
  158. return (((long)data + 128) * 3000) / 256;
  159. }
  160. /*
  161. * For AK8963 and AK09911, same calculation, but the device is less sensitive:
  162. *
  163. * H is in the range of +-8190. The magnetometer has a range of
  164. * +-4912uT. To go from the raw value to uT is:
  165. *
  166. * HuT = H * 4912/8190, or roughly, 6/10, instead of 3/10.
  167. */
  168. static long ak8963_09911_raw_to_gauss(u16 data)
  169. {
  170. return (((long)data + 128) * 6000) / 256;
  171. }
  172. /*
  173. * For AK09912, same calculation, except the device is more sensitive:
  174. *
  175. * H is in the range of -32752 to 32752. The magnetometer has a range of
  176. * +-4912uT. To go from the raw value to uT is:
  177. *
  178. * HuT = H * 4912/32752, or roughly, 3/20, instead of 3/10.
  179. */
  180. static long ak09912_raw_to_gauss(u16 data)
  181. {
  182. return (((long)data + 128) * 1500) / 256;
  183. }
  184. /* Compatible Asahi Kasei Compass parts */
  185. enum asahi_compass_chipset {
  186. AK8975,
  187. AK8963,
  188. AK09911,
  189. AK09912,
  190. AK_MAX_TYPE
  191. };
  192. enum ak_ctrl_reg_addr {
  193. ST1,
  194. ST2,
  195. CNTL,
  196. ASA_BASE,
  197. MAX_REGS,
  198. REGS_END,
  199. };
  200. enum ak_ctrl_reg_mask {
  201. ST1_DRDY,
  202. ST2_HOFL,
  203. ST2_DERR,
  204. CNTL_MODE,
  205. MASK_END,
  206. };
  207. enum ak_ctrl_mode {
  208. POWER_DOWN,
  209. MODE_ONCE,
  210. SELF_TEST,
  211. FUSE_ROM,
  212. MODE_END,
  213. };
  214. struct ak_def {
  215. enum asahi_compass_chipset type;
  216. long (*raw_to_gauss)(u16 data);
  217. u16 range;
  218. u8 ctrl_regs[REGS_END];
  219. u8 ctrl_masks[MASK_END];
  220. u8 ctrl_modes[MODE_END];
  221. u8 data_regs[3];
  222. };
  223. static struct ak_def ak_def_array[AK_MAX_TYPE] = {
  224. {
  225. .type = AK8975,
  226. .raw_to_gauss = ak8975_raw_to_gauss,
  227. .range = 4096,
  228. .ctrl_regs = {
  229. AK8975_REG_ST1,
  230. AK8975_REG_ST2,
  231. AK8975_REG_CNTL,
  232. AK8975_REG_ASAX,
  233. AK8975_MAX_REGS},
  234. .ctrl_masks = {
  235. AK8975_REG_ST1_DRDY_MASK,
  236. AK8975_REG_ST2_HOFL_MASK,
  237. AK8975_REG_ST2_DERR_MASK,
  238. AK8975_REG_CNTL_MODE_MASK},
  239. .ctrl_modes = {
  240. AK8975_REG_CNTL_MODE_POWER_DOWN,
  241. AK8975_REG_CNTL_MODE_ONCE,
  242. AK8975_REG_CNTL_MODE_SELF_TEST,
  243. AK8975_REG_CNTL_MODE_FUSE_ROM},
  244. .data_regs = {
  245. AK8975_REG_HXL,
  246. AK8975_REG_HYL,
  247. AK8975_REG_HZL},
  248. },
  249. {
  250. .type = AK8963,
  251. .raw_to_gauss = ak8963_09911_raw_to_gauss,
  252. .range = 8190,
  253. .ctrl_regs = {
  254. AK8975_REG_ST1,
  255. AK8975_REG_ST2,
  256. AK8975_REG_CNTL,
  257. AK8975_REG_ASAX,
  258. AK8975_MAX_REGS},
  259. .ctrl_masks = {
  260. AK8975_REG_ST1_DRDY_MASK,
  261. AK8975_REG_ST2_HOFL_MASK,
  262. 0,
  263. AK8975_REG_CNTL_MODE_MASK},
  264. .ctrl_modes = {
  265. AK8975_REG_CNTL_MODE_POWER_DOWN,
  266. AK8975_REG_CNTL_MODE_ONCE,
  267. AK8975_REG_CNTL_MODE_SELF_TEST,
  268. AK8975_REG_CNTL_MODE_FUSE_ROM},
  269. .data_regs = {
  270. AK8975_REG_HXL,
  271. AK8975_REG_HYL,
  272. AK8975_REG_HZL},
  273. },
  274. {
  275. .type = AK09911,
  276. .raw_to_gauss = ak8963_09911_raw_to_gauss,
  277. .range = 8192,
  278. .ctrl_regs = {
  279. AK09912_REG_ST1,
  280. AK09912_REG_ST2,
  281. AK09912_REG_CNTL2,
  282. AK09912_REG_ASAX,
  283. AK09912_MAX_REGS},
  284. .ctrl_masks = {
  285. AK09912_REG_ST1_DRDY_MASK,
  286. AK09912_REG_ST2_HOFL_MASK,
  287. 0,
  288. AK09912_REG_CNTL2_MODE_MASK},
  289. .ctrl_modes = {
  290. AK09912_REG_CNTL_MODE_POWER_DOWN,
  291. AK09912_REG_CNTL_MODE_ONCE,
  292. AK09912_REG_CNTL_MODE_SELF_TEST,
  293. AK09912_REG_CNTL_MODE_FUSE_ROM},
  294. .data_regs = {
  295. AK09912_REG_HXL,
  296. AK09912_REG_HYL,
  297. AK09912_REG_HZL},
  298. },
  299. {
  300. .type = AK09912,
  301. .raw_to_gauss = ak09912_raw_to_gauss,
  302. .range = 32752,
  303. .ctrl_regs = {
  304. AK09912_REG_ST1,
  305. AK09912_REG_ST2,
  306. AK09912_REG_CNTL2,
  307. AK09912_REG_ASAX,
  308. AK09912_MAX_REGS},
  309. .ctrl_masks = {
  310. AK09912_REG_ST1_DRDY_MASK,
  311. AK09912_REG_ST2_HOFL_MASK,
  312. 0,
  313. AK09912_REG_CNTL2_MODE_MASK},
  314. .ctrl_modes = {
  315. AK09912_REG_CNTL_MODE_POWER_DOWN,
  316. AK09912_REG_CNTL_MODE_ONCE,
  317. AK09912_REG_CNTL_MODE_SELF_TEST,
  318. AK09912_REG_CNTL_MODE_FUSE_ROM},
  319. .data_regs = {
  320. AK09912_REG_HXL,
  321. AK09912_REG_HYL,
  322. AK09912_REG_HZL},
  323. }
  324. };
  325. /*
  326. * Per-instance context data for the device.
  327. */
  328. struct ak8975_data {
  329. struct i2c_client *client;
  330. struct ak_def *def;
  331. struct attribute_group attrs;
  332. struct mutex lock;
  333. u8 asa[3];
  334. long raw_to_gauss[3];
  335. int eoc_gpio;
  336. int eoc_irq;
  337. wait_queue_head_t data_ready_queue;
  338. unsigned long flags;
  339. u8 cntl_cache;
  340. };
  341. /*
  342. * Return 0 if the i2c device is the one we expect.
  343. * return a negative error number otherwise
  344. */
  345. static int ak8975_who_i_am(struct i2c_client *client,
  346. enum asahi_compass_chipset type)
  347. {
  348. u8 wia_val[2];
  349. int ret;
  350. /*
  351. * Signature for each device:
  352. * Device | WIA1 | WIA2
  353. * AK09912 | DEVICE_ID | AK09912_DEVICE_ID
  354. * AK09911 | DEVICE_ID | AK09911_DEVICE_ID
  355. * AK8975 | DEVICE_ID | NA
  356. * AK8963 | DEVICE_ID | NA
  357. */
  358. ret = i2c_smbus_read_i2c_block_data(client, AK09912_REG_WIA1,
  359. 2, wia_val);
  360. if (ret < 0) {
  361. dev_err(&client->dev, "Error reading WIA\n");
  362. return ret;
  363. }
  364. if (wia_val[0] != AK8975_DEVICE_ID)
  365. return -ENODEV;
  366. switch (type) {
  367. case AK8975:
  368. case AK8963:
  369. return 0;
  370. case AK09911:
  371. if (wia_val[1] == AK09911_DEVICE_ID)
  372. return 0;
  373. break;
  374. case AK09912:
  375. if (wia_val[1] == AK09912_DEVICE_ID)
  376. return 0;
  377. break;
  378. default:
  379. dev_err(&client->dev, "Type %d unknown\n", type);
  380. }
  381. return -ENODEV;
  382. }
  383. /*
  384. * Helper function to write to CNTL register.
  385. */
  386. static int ak8975_set_mode(struct ak8975_data *data, enum ak_ctrl_mode mode)
  387. {
  388. u8 regval;
  389. int ret;
  390. regval = (data->cntl_cache & ~data->def->ctrl_masks[CNTL_MODE]) |
  391. data->def->ctrl_modes[mode];
  392. ret = i2c_smbus_write_byte_data(data->client,
  393. data->def->ctrl_regs[CNTL], regval);
  394. if (ret < 0) {
  395. return ret;
  396. }
  397. data->cntl_cache = regval;
  398. /* After mode change wait atleast 100us */
  399. usleep_range(100, 500);
  400. return 0;
  401. }
  402. /*
  403. * Handle data ready irq
  404. */
  405. static irqreturn_t ak8975_irq_handler(int irq, void *data)
  406. {
  407. struct ak8975_data *ak8975 = data;
  408. set_bit(0, &ak8975->flags);
  409. wake_up(&ak8975->data_ready_queue);
  410. return IRQ_HANDLED;
  411. }
  412. /*
  413. * Install data ready interrupt handler
  414. */
  415. static int ak8975_setup_irq(struct ak8975_data *data)
  416. {
  417. struct i2c_client *client = data->client;
  418. int rc;
  419. int irq;
  420. if (client->irq)
  421. irq = client->irq;
  422. else
  423. irq = gpio_to_irq(data->eoc_gpio);
  424. rc = devm_request_irq(&client->dev, irq, ak8975_irq_handler,
  425. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  426. dev_name(&client->dev), data);
  427. if (rc < 0) {
  428. dev_err(&client->dev,
  429. "irq %d request failed, (gpio %d): %d\n",
  430. irq, data->eoc_gpio, rc);
  431. return rc;
  432. }
  433. init_waitqueue_head(&data->data_ready_queue);
  434. clear_bit(0, &data->flags);
  435. data->eoc_irq = irq;
  436. return rc;
  437. }
  438. /*
  439. * Perform some start-of-day setup, including reading the asa calibration
  440. * values and caching them.
  441. */
  442. static int ak8975_setup(struct i2c_client *client)
  443. {
  444. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  445. struct ak8975_data *data = iio_priv(indio_dev);
  446. int ret;
  447. /* Write the fused rom access mode. */
  448. ret = ak8975_set_mode(data, FUSE_ROM);
  449. if (ret < 0) {
  450. dev_err(&client->dev, "Error in setting fuse access mode\n");
  451. return ret;
  452. }
  453. /* Get asa data and store in the device data. */
  454. ret = i2c_smbus_read_i2c_block_data(client,
  455. data->def->ctrl_regs[ASA_BASE],
  456. 3, data->asa);
  457. if (ret < 0) {
  458. dev_err(&client->dev, "Not able to read asa data\n");
  459. return ret;
  460. }
  461. /* After reading fuse ROM data set power-down mode */
  462. ret = ak8975_set_mode(data, POWER_DOWN);
  463. if (ret < 0) {
  464. dev_err(&client->dev, "Error in setting power-down mode\n");
  465. return ret;
  466. }
  467. if (data->eoc_gpio > 0 || client->irq > 0) {
  468. ret = ak8975_setup_irq(data);
  469. if (ret < 0) {
  470. dev_err(&client->dev,
  471. "Error setting data ready interrupt\n");
  472. return ret;
  473. }
  474. }
  475. data->raw_to_gauss[0] = data->def->raw_to_gauss(data->asa[0]);
  476. data->raw_to_gauss[1] = data->def->raw_to_gauss(data->asa[1]);
  477. data->raw_to_gauss[2] = data->def->raw_to_gauss(data->asa[2]);
  478. return 0;
  479. }
  480. static int wait_conversion_complete_gpio(struct ak8975_data *data)
  481. {
  482. struct i2c_client *client = data->client;
  483. u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
  484. int ret;
  485. /* Wait for the conversion to complete. */
  486. while (timeout_ms) {
  487. msleep(AK8975_CONVERSION_DONE_POLL_TIME);
  488. if (gpio_get_value(data->eoc_gpio))
  489. break;
  490. timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
  491. }
  492. if (!timeout_ms) {
  493. dev_err(&client->dev, "Conversion timeout happened\n");
  494. return -EINVAL;
  495. }
  496. ret = i2c_smbus_read_byte_data(client, data->def->ctrl_regs[ST1]);
  497. if (ret < 0)
  498. dev_err(&client->dev, "Error in reading ST1\n");
  499. return ret;
  500. }
  501. static int wait_conversion_complete_polled(struct ak8975_data *data)
  502. {
  503. struct i2c_client *client = data->client;
  504. u8 read_status;
  505. u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
  506. int ret;
  507. /* Wait for the conversion to complete. */
  508. while (timeout_ms) {
  509. msleep(AK8975_CONVERSION_DONE_POLL_TIME);
  510. ret = i2c_smbus_read_byte_data(client,
  511. data->def->ctrl_regs[ST1]);
  512. if (ret < 0) {
  513. dev_err(&client->dev, "Error in reading ST1\n");
  514. return ret;
  515. }
  516. read_status = ret;
  517. if (read_status)
  518. break;
  519. timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
  520. }
  521. if (!timeout_ms) {
  522. dev_err(&client->dev, "Conversion timeout happened\n");
  523. return -EINVAL;
  524. }
  525. return read_status;
  526. }
  527. /* Returns 0 if the end of conversion interrupt occured or -ETIME otherwise */
  528. static int wait_conversion_complete_interrupt(struct ak8975_data *data)
  529. {
  530. int ret;
  531. ret = wait_event_timeout(data->data_ready_queue,
  532. test_bit(0, &data->flags),
  533. AK8975_DATA_READY_TIMEOUT);
  534. clear_bit(0, &data->flags);
  535. return ret > 0 ? 0 : -ETIME;
  536. }
  537. /*
  538. * Emits the raw flux value for the x, y, or z axis.
  539. */
  540. static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
  541. {
  542. struct ak8975_data *data = iio_priv(indio_dev);
  543. struct i2c_client *client = data->client;
  544. int ret;
  545. mutex_lock(&data->lock);
  546. /* Set up the device for taking a sample. */
  547. ret = ak8975_set_mode(data, MODE_ONCE);
  548. if (ret < 0) {
  549. dev_err(&client->dev, "Error in setting operating mode\n");
  550. goto exit;
  551. }
  552. /* Wait for the conversion to complete. */
  553. if (data->eoc_irq)
  554. ret = wait_conversion_complete_interrupt(data);
  555. else if (gpio_is_valid(data->eoc_gpio))
  556. ret = wait_conversion_complete_gpio(data);
  557. else
  558. ret = wait_conversion_complete_polled(data);
  559. if (ret < 0)
  560. goto exit;
  561. /* This will be executed only for non-interrupt based waiting case */
  562. if (ret & data->def->ctrl_masks[ST1_DRDY]) {
  563. ret = i2c_smbus_read_byte_data(client,
  564. data->def->ctrl_regs[ST2]);
  565. if (ret < 0) {
  566. dev_err(&client->dev, "Error in reading ST2\n");
  567. goto exit;
  568. }
  569. if (ret & (data->def->ctrl_masks[ST2_DERR] |
  570. data->def->ctrl_masks[ST2_HOFL])) {
  571. dev_err(&client->dev, "ST2 status error 0x%x\n", ret);
  572. ret = -EINVAL;
  573. goto exit;
  574. }
  575. }
  576. /* Read the flux value from the appropriate register
  577. (the register is specified in the iio device attributes). */
  578. ret = i2c_smbus_read_word_data(client, data->def->data_regs[index]);
  579. if (ret < 0) {
  580. dev_err(&client->dev, "Read axis data fails\n");
  581. goto exit;
  582. }
  583. mutex_unlock(&data->lock);
  584. /* Clamp to valid range. */
  585. *val = clamp_t(s16, ret, -data->def->range, data->def->range);
  586. return IIO_VAL_INT;
  587. exit:
  588. mutex_unlock(&data->lock);
  589. return ret;
  590. }
  591. static int ak8975_read_raw(struct iio_dev *indio_dev,
  592. struct iio_chan_spec const *chan,
  593. int *val, int *val2,
  594. long mask)
  595. {
  596. struct ak8975_data *data = iio_priv(indio_dev);
  597. switch (mask) {
  598. case IIO_CHAN_INFO_RAW:
  599. return ak8975_read_axis(indio_dev, chan->address, val);
  600. case IIO_CHAN_INFO_SCALE:
  601. *val = 0;
  602. *val2 = data->raw_to_gauss[chan->address];
  603. return IIO_VAL_INT_PLUS_MICRO;
  604. }
  605. return -EINVAL;
  606. }
  607. #define AK8975_CHANNEL(axis, index) \
  608. { \
  609. .type = IIO_MAGN, \
  610. .modified = 1, \
  611. .channel2 = IIO_MOD_##axis, \
  612. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  613. BIT(IIO_CHAN_INFO_SCALE), \
  614. .address = index, \
  615. }
  616. static const struct iio_chan_spec ak8975_channels[] = {
  617. AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2),
  618. };
  619. static const struct iio_info ak8975_info = {
  620. .read_raw = &ak8975_read_raw,
  621. .driver_module = THIS_MODULE,
  622. };
  623. static const struct acpi_device_id ak_acpi_match[] = {
  624. {"AK8975", AK8975},
  625. {"AK8963", AK8963},
  626. {"INVN6500", AK8963},
  627. {"AK09911", AK09911},
  628. {"AK09912", AK09912},
  629. { },
  630. };
  631. MODULE_DEVICE_TABLE(acpi, ak_acpi_match);
  632. static const char *ak8975_match_acpi_device(struct device *dev,
  633. enum asahi_compass_chipset *chipset)
  634. {
  635. const struct acpi_device_id *id;
  636. id = acpi_match_device(dev->driver->acpi_match_table, dev);
  637. if (!id)
  638. return NULL;
  639. *chipset = (int)id->driver_data;
  640. return dev_name(dev);
  641. }
  642. static int ak8975_probe(struct i2c_client *client,
  643. const struct i2c_device_id *id)
  644. {
  645. struct ak8975_data *data;
  646. struct iio_dev *indio_dev;
  647. int eoc_gpio;
  648. int err;
  649. const char *name = NULL;
  650. enum asahi_compass_chipset chipset;
  651. /* Grab and set up the supplied GPIO. */
  652. if (client->dev.platform_data)
  653. eoc_gpio = *(int *)(client->dev.platform_data);
  654. else if (client->dev.of_node)
  655. eoc_gpio = of_get_gpio(client->dev.of_node, 0);
  656. else
  657. eoc_gpio = -1;
  658. if (eoc_gpio == -EPROBE_DEFER)
  659. return -EPROBE_DEFER;
  660. /* We may not have a GPIO based IRQ to scan, that is fine, we will
  661. poll if so */
  662. if (gpio_is_valid(eoc_gpio)) {
  663. err = devm_gpio_request_one(&client->dev, eoc_gpio,
  664. GPIOF_IN, "ak_8975");
  665. if (err < 0) {
  666. dev_err(&client->dev,
  667. "failed to request GPIO %d, error %d\n",
  668. eoc_gpio, err);
  669. return err;
  670. }
  671. }
  672. /* Register with IIO */
  673. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  674. if (indio_dev == NULL)
  675. return -ENOMEM;
  676. data = iio_priv(indio_dev);
  677. i2c_set_clientdata(client, indio_dev);
  678. data->client = client;
  679. data->eoc_gpio = eoc_gpio;
  680. data->eoc_irq = 0;
  681. /* id will be NULL when enumerated via ACPI */
  682. if (id) {
  683. chipset = (enum asahi_compass_chipset)(id->driver_data);
  684. name = id->name;
  685. } else if (ACPI_HANDLE(&client->dev))
  686. name = ak8975_match_acpi_device(&client->dev, &chipset);
  687. else
  688. return -ENOSYS;
  689. if (chipset >= AK_MAX_TYPE) {
  690. dev_err(&client->dev, "AKM device type unsupported: %d\n",
  691. chipset);
  692. return -ENODEV;
  693. }
  694. data->def = &ak_def_array[chipset];
  695. err = ak8975_who_i_am(client, data->def->type);
  696. if (err < 0) {
  697. dev_err(&client->dev, "Unexpected device\n");
  698. return err;
  699. }
  700. dev_dbg(&client->dev, "Asahi compass chip %s\n", name);
  701. /* Perform some basic start-of-day setup of the device. */
  702. err = ak8975_setup(client);
  703. if (err < 0) {
  704. dev_err(&client->dev, "%s initialization fails\n", name);
  705. return err;
  706. }
  707. mutex_init(&data->lock);
  708. indio_dev->dev.parent = &client->dev;
  709. indio_dev->channels = ak8975_channels;
  710. indio_dev->num_channels = ARRAY_SIZE(ak8975_channels);
  711. indio_dev->info = &ak8975_info;
  712. indio_dev->modes = INDIO_DIRECT_MODE;
  713. indio_dev->name = name;
  714. return devm_iio_device_register(&client->dev, indio_dev);
  715. }
  716. static const struct i2c_device_id ak8975_id[] = {
  717. {"ak8975", AK8975},
  718. {"ak8963", AK8963},
  719. {"AK8963", AK8963},
  720. {"ak09911", AK09911},
  721. {"ak09912", AK09912},
  722. {}
  723. };
  724. MODULE_DEVICE_TABLE(i2c, ak8975_id);
  725. static const struct of_device_id ak8975_of_match[] = {
  726. { .compatible = "asahi-kasei,ak8975", },
  727. { .compatible = "ak8975", },
  728. { .compatible = "asahi-kasei,ak8963", },
  729. { .compatible = "ak8963", },
  730. { .compatible = "asahi-kasei,ak09911", },
  731. { .compatible = "ak09911", },
  732. { .compatible = "asahi-kasei,ak09912", },
  733. { .compatible = "ak09912", },
  734. {}
  735. };
  736. MODULE_DEVICE_TABLE(of, ak8975_of_match);
  737. static struct i2c_driver ak8975_driver = {
  738. .driver = {
  739. .name = "ak8975",
  740. .of_match_table = of_match_ptr(ak8975_of_match),
  741. .acpi_match_table = ACPI_PTR(ak_acpi_match),
  742. },
  743. .probe = ak8975_probe,
  744. .id_table = ak8975_id,
  745. };
  746. module_i2c_driver(ak8975_driver);
  747. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  748. MODULE_DESCRIPTION("AK8975 magnetometer driver");
  749. MODULE_LICENSE("GPL");