ak8975.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  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. u16 buff;
  617. int ret;
  618. pm_runtime_get_sync(&data->client->dev);
  619. mutex_lock(&data->lock);
  620. ret = ak8975_start_read_axis(data, client);
  621. if (ret)
  622. goto exit;
  623. ret = i2c_smbus_read_i2c_block_data_or_emulated(
  624. client, def->data_regs[index],
  625. sizeof(buff), (u8*)&buff);
  626. if (ret < 0)
  627. goto exit;
  628. mutex_unlock(&data->lock);
  629. pm_runtime_mark_last_busy(&data->client->dev);
  630. pm_runtime_put_autosuspend(&data->client->dev);
  631. /* Swap bytes and convert to valid range. */
  632. buff = le16_to_cpu(buff);
  633. *val = clamp_t(s16, buff, -def->range, def->range);
  634. return IIO_VAL_INT;
  635. exit:
  636. mutex_unlock(&data->lock);
  637. dev_err(&client->dev, "Error in reading axis\n");
  638. return ret;
  639. }
  640. static int ak8975_read_raw(struct iio_dev *indio_dev,
  641. struct iio_chan_spec const *chan,
  642. int *val, int *val2,
  643. long mask)
  644. {
  645. struct ak8975_data *data = iio_priv(indio_dev);
  646. switch (mask) {
  647. case IIO_CHAN_INFO_RAW:
  648. return ak8975_read_axis(indio_dev, chan->address, val);
  649. case IIO_CHAN_INFO_SCALE:
  650. *val = 0;
  651. *val2 = data->raw_to_gauss[chan->address];
  652. return IIO_VAL_INT_PLUS_MICRO;
  653. }
  654. return -EINVAL;
  655. }
  656. static const struct iio_mount_matrix *
  657. ak8975_get_mount_matrix(const struct iio_dev *indio_dev,
  658. const struct iio_chan_spec *chan)
  659. {
  660. return &((struct ak8975_data *)iio_priv(indio_dev))->orientation;
  661. }
  662. static const struct iio_chan_spec_ext_info ak8975_ext_info[] = {
  663. IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, ak8975_get_mount_matrix),
  664. { },
  665. };
  666. #define AK8975_CHANNEL(axis, index) \
  667. { \
  668. .type = IIO_MAGN, \
  669. .modified = 1, \
  670. .channel2 = IIO_MOD_##axis, \
  671. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  672. BIT(IIO_CHAN_INFO_SCALE), \
  673. .address = index, \
  674. .scan_index = index, \
  675. .scan_type = { \
  676. .sign = 's', \
  677. .realbits = 16, \
  678. .storagebits = 16, \
  679. .endianness = IIO_CPU \
  680. }, \
  681. .ext_info = ak8975_ext_info, \
  682. }
  683. static const struct iio_chan_spec ak8975_channels[] = {
  684. AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2),
  685. IIO_CHAN_SOFT_TIMESTAMP(3),
  686. };
  687. static const unsigned long ak8975_scan_masks[] = { 0x7, 0 };
  688. static const struct iio_info ak8975_info = {
  689. .read_raw = &ak8975_read_raw,
  690. .driver_module = THIS_MODULE,
  691. };
  692. static const struct acpi_device_id ak_acpi_match[] = {
  693. {"AK8975", AK8975},
  694. {"AK8963", AK8963},
  695. {"INVN6500", AK8963},
  696. {"AK09911", AK09911},
  697. {"AK09912", AK09912},
  698. { },
  699. };
  700. MODULE_DEVICE_TABLE(acpi, ak_acpi_match);
  701. static const char *ak8975_match_acpi_device(struct device *dev,
  702. enum asahi_compass_chipset *chipset)
  703. {
  704. const struct acpi_device_id *id;
  705. id = acpi_match_device(dev->driver->acpi_match_table, dev);
  706. if (!id)
  707. return NULL;
  708. *chipset = (int)id->driver_data;
  709. return dev_name(dev);
  710. }
  711. static void ak8975_fill_buffer(struct iio_dev *indio_dev)
  712. {
  713. struct ak8975_data *data = iio_priv(indio_dev);
  714. const struct i2c_client *client = data->client;
  715. const struct ak_def *def = data->def;
  716. int ret;
  717. s16 buff[8]; /* 3 x 16 bits axis values + 1 aligned 64 bits timestamp */
  718. mutex_lock(&data->lock);
  719. ret = ak8975_start_read_axis(data, client);
  720. if (ret)
  721. goto unlock;
  722. /*
  723. * For each axis, read the flux value from the appropriate register
  724. * (the register is specified in the iio device attributes).
  725. */
  726. ret = i2c_smbus_read_i2c_block_data_or_emulated(client,
  727. def->data_regs[0],
  728. 3 * sizeof(buff[0]),
  729. (u8 *)buff);
  730. if (ret < 0)
  731. goto unlock;
  732. mutex_unlock(&data->lock);
  733. /* Clamp to valid range. */
  734. buff[0] = clamp_t(s16, le16_to_cpu(buff[0]), -def->range, def->range);
  735. buff[1] = clamp_t(s16, le16_to_cpu(buff[1]), -def->range, def->range);
  736. buff[2] = clamp_t(s16, le16_to_cpu(buff[2]), -def->range, def->range);
  737. iio_push_to_buffers_with_timestamp(indio_dev, buff,
  738. iio_get_time_ns(indio_dev));
  739. return;
  740. unlock:
  741. mutex_unlock(&data->lock);
  742. dev_err(&client->dev, "Error in reading axes block\n");
  743. }
  744. static irqreturn_t ak8975_handle_trigger(int irq, void *p)
  745. {
  746. const struct iio_poll_func *pf = p;
  747. struct iio_dev *indio_dev = pf->indio_dev;
  748. ak8975_fill_buffer(indio_dev);
  749. iio_trigger_notify_done(indio_dev->trig);
  750. return IRQ_HANDLED;
  751. }
  752. static int ak8975_probe(struct i2c_client *client,
  753. const struct i2c_device_id *id)
  754. {
  755. struct ak8975_data *data;
  756. struct iio_dev *indio_dev;
  757. int eoc_gpio;
  758. int err;
  759. const char *name = NULL;
  760. enum asahi_compass_chipset chipset = AK_MAX_TYPE;
  761. const struct ak8975_platform_data *pdata =
  762. dev_get_platdata(&client->dev);
  763. /* Grab and set up the supplied GPIO. */
  764. if (pdata)
  765. eoc_gpio = pdata->eoc_gpio;
  766. else if (client->dev.of_node)
  767. eoc_gpio = of_get_gpio(client->dev.of_node, 0);
  768. else
  769. eoc_gpio = -1;
  770. if (eoc_gpio == -EPROBE_DEFER)
  771. return -EPROBE_DEFER;
  772. /* We may not have a GPIO based IRQ to scan, that is fine, we will
  773. poll if so */
  774. if (gpio_is_valid(eoc_gpio)) {
  775. err = devm_gpio_request_one(&client->dev, eoc_gpio,
  776. GPIOF_IN, "ak_8975");
  777. if (err < 0) {
  778. dev_err(&client->dev,
  779. "failed to request GPIO %d, error %d\n",
  780. eoc_gpio, err);
  781. return err;
  782. }
  783. }
  784. /* Register with IIO */
  785. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  786. if (indio_dev == NULL)
  787. return -ENOMEM;
  788. data = iio_priv(indio_dev);
  789. i2c_set_clientdata(client, indio_dev);
  790. data->client = client;
  791. data->eoc_gpio = eoc_gpio;
  792. data->eoc_irq = 0;
  793. if (!pdata) {
  794. err = of_iio_read_mount_matrix(&client->dev,
  795. "mount-matrix",
  796. &data->orientation);
  797. if (err)
  798. return err;
  799. } else
  800. data->orientation = pdata->orientation;
  801. /* id will be NULL when enumerated via ACPI */
  802. if (id) {
  803. chipset = (enum asahi_compass_chipset)(id->driver_data);
  804. name = id->name;
  805. } else if (ACPI_HANDLE(&client->dev)) {
  806. name = ak8975_match_acpi_device(&client->dev, &chipset);
  807. if (!name)
  808. return -ENODEV;
  809. } else
  810. return -ENOSYS;
  811. if (chipset >= AK_MAX_TYPE) {
  812. dev_err(&client->dev, "AKM device type unsupported: %d\n",
  813. chipset);
  814. return -ENODEV;
  815. }
  816. data->def = &ak_def_array[chipset];
  817. /* Fetch the regulators */
  818. data->vdd = devm_regulator_get(&client->dev, "vdd");
  819. if (IS_ERR(data->vdd))
  820. return PTR_ERR(data->vdd);
  821. data->vid = devm_regulator_get(&client->dev, "vid");
  822. if (IS_ERR(data->vid))
  823. return PTR_ERR(data->vid);
  824. err = ak8975_power_on(data);
  825. if (err)
  826. return err;
  827. err = ak8975_who_i_am(client, data->def->type);
  828. if (err < 0) {
  829. dev_err(&client->dev, "Unexpected device\n");
  830. goto power_off;
  831. }
  832. dev_dbg(&client->dev, "Asahi compass chip %s\n", name);
  833. /* Perform some basic start-of-day setup of the device. */
  834. err = ak8975_setup(client);
  835. if (err < 0) {
  836. dev_err(&client->dev, "%s initialization fails\n", name);
  837. goto power_off;
  838. }
  839. mutex_init(&data->lock);
  840. indio_dev->dev.parent = &client->dev;
  841. indio_dev->channels = ak8975_channels;
  842. indio_dev->num_channels = ARRAY_SIZE(ak8975_channels);
  843. indio_dev->info = &ak8975_info;
  844. indio_dev->available_scan_masks = ak8975_scan_masks;
  845. indio_dev->modes = INDIO_DIRECT_MODE;
  846. indio_dev->name = name;
  847. err = iio_triggered_buffer_setup(indio_dev, NULL, ak8975_handle_trigger,
  848. NULL);
  849. if (err) {
  850. dev_err(&client->dev, "triggered buffer setup failed\n");
  851. goto power_off;
  852. }
  853. err = iio_device_register(indio_dev);
  854. if (err) {
  855. dev_err(&client->dev, "device register failed\n");
  856. goto cleanup_buffer;
  857. }
  858. /* Enable runtime PM */
  859. pm_runtime_get_noresume(&client->dev);
  860. pm_runtime_set_active(&client->dev);
  861. pm_runtime_enable(&client->dev);
  862. /*
  863. * The device comes online in 500us, so add two orders of magnitude
  864. * of delay before autosuspending: 50 ms.
  865. */
  866. pm_runtime_set_autosuspend_delay(&client->dev, 50);
  867. pm_runtime_use_autosuspend(&client->dev);
  868. pm_runtime_put(&client->dev);
  869. return 0;
  870. cleanup_buffer:
  871. iio_triggered_buffer_cleanup(indio_dev);
  872. power_off:
  873. ak8975_power_off(data);
  874. return err;
  875. }
  876. static int ak8975_remove(struct i2c_client *client)
  877. {
  878. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  879. struct ak8975_data *data = iio_priv(indio_dev);
  880. pm_runtime_get_sync(&client->dev);
  881. pm_runtime_put_noidle(&client->dev);
  882. pm_runtime_disable(&client->dev);
  883. iio_device_unregister(indio_dev);
  884. iio_triggered_buffer_cleanup(indio_dev);
  885. ak8975_set_mode(data, POWER_DOWN);
  886. ak8975_power_off(data);
  887. return 0;
  888. }
  889. #ifdef CONFIG_PM
  890. static int ak8975_runtime_suspend(struct device *dev)
  891. {
  892. struct i2c_client *client = to_i2c_client(dev);
  893. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  894. struct ak8975_data *data = iio_priv(indio_dev);
  895. int ret;
  896. /* Set the device in power down if it wasn't already */
  897. ret = ak8975_set_mode(data, POWER_DOWN);
  898. if (ret < 0) {
  899. dev_err(&client->dev, "Error in setting power-down mode\n");
  900. return ret;
  901. }
  902. /* Next cut the regulators */
  903. ak8975_power_off(data);
  904. return 0;
  905. }
  906. static int ak8975_runtime_resume(struct device *dev)
  907. {
  908. struct i2c_client *client = to_i2c_client(dev);
  909. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  910. struct ak8975_data *data = iio_priv(indio_dev);
  911. int ret;
  912. /* Take up the regulators */
  913. ak8975_power_on(data);
  914. /*
  915. * We come up in powered down mode, the reading routines will
  916. * put us in the mode to read values later.
  917. */
  918. ret = ak8975_set_mode(data, POWER_DOWN);
  919. if (ret < 0) {
  920. dev_err(&client->dev, "Error in setting power-down mode\n");
  921. return ret;
  922. }
  923. return 0;
  924. }
  925. #endif /* CONFIG_PM */
  926. static const struct dev_pm_ops ak8975_dev_pm_ops = {
  927. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  928. pm_runtime_force_resume)
  929. SET_RUNTIME_PM_OPS(ak8975_runtime_suspend,
  930. ak8975_runtime_resume, NULL)
  931. };
  932. static const struct i2c_device_id ak8975_id[] = {
  933. {"ak8975", AK8975},
  934. {"ak8963", AK8963},
  935. {"AK8963", AK8963},
  936. {"ak09911", AK09911},
  937. {"ak09912", AK09912},
  938. {}
  939. };
  940. MODULE_DEVICE_TABLE(i2c, ak8975_id);
  941. static const struct of_device_id ak8975_of_match[] = {
  942. { .compatible = "asahi-kasei,ak8975", },
  943. { .compatible = "ak8975", },
  944. { .compatible = "asahi-kasei,ak8963", },
  945. { .compatible = "ak8963", },
  946. { .compatible = "asahi-kasei,ak09911", },
  947. { .compatible = "ak09911", },
  948. { .compatible = "asahi-kasei,ak09912", },
  949. { .compatible = "ak09912", },
  950. {}
  951. };
  952. MODULE_DEVICE_TABLE(of, ak8975_of_match);
  953. static struct i2c_driver ak8975_driver = {
  954. .driver = {
  955. .name = "ak8975",
  956. .pm = &ak8975_dev_pm_ops,
  957. .of_match_table = of_match_ptr(ak8975_of_match),
  958. .acpi_match_table = ACPI_PTR(ak_acpi_match),
  959. },
  960. .probe = ak8975_probe,
  961. .remove = ak8975_remove,
  962. .id_table = ak8975_id,
  963. };
  964. module_i2c_driver(ak8975_driver);
  965. MODULE_AUTHOR("Laxman Dewangan <[email protected]>");
  966. MODULE_DESCRIPTION("AK8975 magnetometer driver");
  967. MODULE_LICENSE("GPL");