ak8975.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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/iio/iio.h>
  34. #include <linux/iio/sysfs.h>
  35. /*
  36. * Register definitions, as well as various shifts and masks to get at the
  37. * individual fields of the registers.
  38. */
  39. #define AK8975_REG_WIA 0x00
  40. #define AK8975_DEVICE_ID 0x48
  41. #define AK8975_REG_INFO 0x01
  42. #define AK8975_REG_ST1 0x02
  43. #define AK8975_REG_ST1_DRDY_SHIFT 0
  44. #define AK8975_REG_ST1_DRDY_MASK (1 << AK8975_REG_ST1_DRDY_SHIFT)
  45. #define AK8975_REG_HXL 0x03
  46. #define AK8975_REG_HXH 0x04
  47. #define AK8975_REG_HYL 0x05
  48. #define AK8975_REG_HYH 0x06
  49. #define AK8975_REG_HZL 0x07
  50. #define AK8975_REG_HZH 0x08
  51. #define AK8975_REG_ST2 0x09
  52. #define AK8975_REG_ST2_DERR_SHIFT 2
  53. #define AK8975_REG_ST2_DERR_MASK (1 << AK8975_REG_ST2_DERR_SHIFT)
  54. #define AK8975_REG_ST2_HOFL_SHIFT 3
  55. #define AK8975_REG_ST2_HOFL_MASK (1 << AK8975_REG_ST2_HOFL_SHIFT)
  56. #define AK8975_REG_CNTL 0x0A
  57. #define AK8975_REG_CNTL_MODE_SHIFT 0
  58. #define AK8975_REG_CNTL_MODE_MASK (0xF << AK8975_REG_CNTL_MODE_SHIFT)
  59. #define AK8975_REG_CNTL_MODE_POWER_DOWN 0
  60. #define AK8975_REG_CNTL_MODE_ONCE 1
  61. #define AK8975_REG_CNTL_MODE_SELF_TEST 8
  62. #define AK8975_REG_CNTL_MODE_FUSE_ROM 0xF
  63. #define AK8975_REG_RSVC 0x0B
  64. #define AK8975_REG_ASTC 0x0C
  65. #define AK8975_REG_TS1 0x0D
  66. #define AK8975_REG_TS2 0x0E
  67. #define AK8975_REG_I2CDIS 0x0F
  68. #define AK8975_REG_ASAX 0x10
  69. #define AK8975_REG_ASAY 0x11
  70. #define AK8975_REG_ASAZ 0x12
  71. #define AK8975_MAX_REGS AK8975_REG_ASAZ
  72. /*
  73. * Miscellaneous values.
  74. */
  75. #define AK8975_MAX_CONVERSION_TIMEOUT 500
  76. #define AK8975_CONVERSION_DONE_POLL_TIME 10
  77. #define AK8975_DATA_READY_TIMEOUT ((100*HZ)/1000)
  78. /*
  79. * Per-instance context data for the device.
  80. */
  81. struct ak8975_data {
  82. struct i2c_client *client;
  83. struct attribute_group attrs;
  84. struct mutex lock;
  85. u8 asa[3];
  86. long raw_to_gauss[3];
  87. u8 reg_cache[AK8975_MAX_REGS];
  88. int eoc_gpio;
  89. int eoc_irq;
  90. wait_queue_head_t data_ready_queue;
  91. unsigned long flags;
  92. };
  93. static const int ak8975_index_to_reg[] = {
  94. AK8975_REG_HXL, AK8975_REG_HYL, AK8975_REG_HZL,
  95. };
  96. /*
  97. * Helper function to write to the I2C device's registers.
  98. */
  99. static int ak8975_write_data(struct i2c_client *client,
  100. u8 reg, u8 val, u8 mask, u8 shift)
  101. {
  102. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  103. struct ak8975_data *data = iio_priv(indio_dev);
  104. u8 regval;
  105. int ret;
  106. regval = (data->reg_cache[reg] & ~mask) | (val << shift);
  107. ret = i2c_smbus_write_byte_data(client, reg, regval);
  108. if (ret < 0) {
  109. dev_err(&client->dev, "Write to device fails status %x\n", ret);
  110. return ret;
  111. }
  112. data->reg_cache[reg] = regval;
  113. return 0;
  114. }
  115. /*
  116. * Handle data ready irq
  117. */
  118. static irqreturn_t ak8975_irq_handler(int irq, void *data)
  119. {
  120. struct ak8975_data *ak8975 = data;
  121. set_bit(0, &ak8975->flags);
  122. wake_up(&ak8975->data_ready_queue);
  123. return IRQ_HANDLED;
  124. }
  125. /*
  126. * Install data ready interrupt handler
  127. */
  128. static int ak8975_setup_irq(struct ak8975_data *data)
  129. {
  130. struct i2c_client *client = data->client;
  131. int rc;
  132. int irq;
  133. if (client->irq)
  134. irq = client->irq;
  135. else
  136. irq = gpio_to_irq(data->eoc_gpio);
  137. rc = request_irq(irq, ak8975_irq_handler,
  138. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  139. dev_name(&client->dev), data);
  140. if (rc < 0) {
  141. dev_err(&client->dev,
  142. "irq %d request failed, (gpio %d): %d\n",
  143. irq, data->eoc_gpio, rc);
  144. return rc;
  145. }
  146. init_waitqueue_head(&data->data_ready_queue);
  147. clear_bit(0, &data->flags);
  148. data->eoc_irq = irq;
  149. return rc;
  150. }
  151. /*
  152. * Perform some start-of-day setup, including reading the asa calibration
  153. * values and caching them.
  154. */
  155. static int ak8975_setup(struct i2c_client *client)
  156. {
  157. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  158. struct ak8975_data *data = iio_priv(indio_dev);
  159. u8 device_id;
  160. int ret;
  161. /* Confirm that the device we're talking to is really an AK8975. */
  162. ret = i2c_smbus_read_byte_data(client, AK8975_REG_WIA);
  163. if (ret < 0) {
  164. dev_err(&client->dev, "Error reading WIA\n");
  165. return ret;
  166. }
  167. device_id = ret;
  168. if (device_id != AK8975_DEVICE_ID) {
  169. dev_err(&client->dev, "Device ak8975 not found\n");
  170. return -ENODEV;
  171. }
  172. /* Write the fused rom access mode. */
  173. ret = ak8975_write_data(client,
  174. AK8975_REG_CNTL,
  175. AK8975_REG_CNTL_MODE_FUSE_ROM,
  176. AK8975_REG_CNTL_MODE_MASK,
  177. AK8975_REG_CNTL_MODE_SHIFT);
  178. if (ret < 0) {
  179. dev_err(&client->dev, "Error in setting fuse access mode\n");
  180. return ret;
  181. }
  182. /* Get asa data and store in the device data. */
  183. ret = i2c_smbus_read_i2c_block_data(client, AK8975_REG_ASAX,
  184. 3, data->asa);
  185. if (ret < 0) {
  186. dev_err(&client->dev, "Not able to read asa data\n");
  187. return ret;
  188. }
  189. /* After reading fuse ROM data set power-down mode */
  190. ret = ak8975_write_data(client,
  191. AK8975_REG_CNTL,
  192. AK8975_REG_CNTL_MODE_POWER_DOWN,
  193. AK8975_REG_CNTL_MODE_MASK,
  194. AK8975_REG_CNTL_MODE_SHIFT);
  195. if (data->eoc_gpio > 0 || client->irq) {
  196. ret = ak8975_setup_irq(data);
  197. if (ret < 0) {
  198. dev_err(&client->dev,
  199. "Error setting data ready interrupt\n");
  200. return ret;
  201. }
  202. }
  203. if (ret < 0) {
  204. dev_err(&client->dev, "Error in setting power-down mode\n");
  205. return ret;
  206. }
  207. /*
  208. * Precalculate scale factor (in Gauss units) for each axis and
  209. * store in the device data.
  210. *
  211. * This scale factor is axis-dependent, and is derived from 3 calibration
  212. * factors ASA(x), ASA(y), and ASA(z).
  213. *
  214. * These ASA values are read from the sensor device at start of day, and
  215. * cached in the device context struct.
  216. *
  217. * Adjusting the flux value with the sensitivity adjustment value should be
  218. * done via the following formula:
  219. *
  220. * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 )
  221. *
  222. * where H is the raw value, ASA is the sensitivity adjustment, and Hadj
  223. * is the resultant adjusted value.
  224. *
  225. * We reduce the formula to:
  226. *
  227. * Hadj = H * (ASA + 128) / 256
  228. *
  229. * H is in the range of -4096 to 4095. The magnetometer has a range of
  230. * +-1229uT. To go from the raw value to uT is:
  231. *
  232. * HuT = H * 1229/4096, or roughly, 3/10.
  233. *
  234. * Since 1uT = 100 gauss, our final scale factor becomes:
  235. *
  236. * Hadj = H * ((ASA + 128) / 256) * 3/10 * 100
  237. * Hadj = H * ((ASA + 128) * 30 / 256
  238. *
  239. * Since ASA doesn't change, we cache the resultant scale factor into the
  240. * device context in ak8975_setup().
  241. */
  242. data->raw_to_gauss[0] = ((data->asa[0] + 128) * 30) >> 8;
  243. data->raw_to_gauss[1] = ((data->asa[1] + 128) * 30) >> 8;
  244. data->raw_to_gauss[2] = ((data->asa[2] + 128) * 30) >> 8;
  245. return 0;
  246. }
  247. static int wait_conversion_complete_gpio(struct ak8975_data *data)
  248. {
  249. struct i2c_client *client = data->client;
  250. u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
  251. int ret;
  252. /* Wait for the conversion to complete. */
  253. while (timeout_ms) {
  254. msleep(AK8975_CONVERSION_DONE_POLL_TIME);
  255. if (gpio_get_value(data->eoc_gpio))
  256. break;
  257. timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
  258. }
  259. if (!timeout_ms) {
  260. dev_err(&client->dev, "Conversion timeout happened\n");
  261. return -EINVAL;
  262. }
  263. ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST1);
  264. if (ret < 0)
  265. dev_err(&client->dev, "Error in reading ST1\n");
  266. return ret;
  267. }
  268. static int wait_conversion_complete_polled(struct ak8975_data *data)
  269. {
  270. struct i2c_client *client = data->client;
  271. u8 read_status;
  272. u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
  273. int ret;
  274. /* Wait for the conversion to complete. */
  275. while (timeout_ms) {
  276. msleep(AK8975_CONVERSION_DONE_POLL_TIME);
  277. ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST1);
  278. if (ret < 0) {
  279. dev_err(&client->dev, "Error in reading ST1\n");
  280. return ret;
  281. }
  282. read_status = ret;
  283. if (read_status)
  284. break;
  285. timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
  286. }
  287. if (!timeout_ms) {
  288. dev_err(&client->dev, "Conversion timeout happened\n");
  289. return -EINVAL;
  290. }
  291. return read_status;
  292. }
  293. /* Returns 0 if the end of conversion interrupt occured or -ETIME otherwise */
  294. static int wait_conversion_complete_interrupt(struct ak8975_data *data)
  295. {
  296. int ret;
  297. ret = wait_event_timeout(data->data_ready_queue,
  298. test_bit(0, &data->flags),
  299. AK8975_DATA_READY_TIMEOUT);
  300. clear_bit(0, &data->flags);
  301. return ret > 0 ? 0 : -ETIME;
  302. }
  303. /*
  304. * Emits the raw flux value for the x, y, or z axis.
  305. */
  306. static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
  307. {
  308. struct ak8975_data *data = iio_priv(indio_dev);
  309. struct i2c_client *client = data->client;
  310. u16 meas_reg;
  311. s16 raw;
  312. int ret;
  313. mutex_lock(&data->lock);
  314. /* Set up the device for taking a sample. */
  315. ret = ak8975_write_data(client,
  316. AK8975_REG_CNTL,
  317. AK8975_REG_CNTL_MODE_ONCE,
  318. AK8975_REG_CNTL_MODE_MASK,
  319. AK8975_REG_CNTL_MODE_SHIFT);
  320. if (ret < 0) {
  321. dev_err(&client->dev, "Error in setting operating mode\n");
  322. goto exit;
  323. }
  324. /* Wait for the conversion to complete. */
  325. if (data->eoc_irq)
  326. ret = wait_conversion_complete_interrupt(data);
  327. else if (gpio_is_valid(data->eoc_gpio))
  328. ret = wait_conversion_complete_gpio(data);
  329. else
  330. ret = wait_conversion_complete_polled(data);
  331. if (ret < 0)
  332. goto exit;
  333. /* This will be executed only for non-interrupt based waiting case */
  334. if (ret & AK8975_REG_ST1_DRDY_MASK) {
  335. ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST2);
  336. if (ret < 0) {
  337. dev_err(&client->dev, "Error in reading ST2\n");
  338. goto exit;
  339. }
  340. if (ret & (AK8975_REG_ST2_DERR_MASK |
  341. AK8975_REG_ST2_HOFL_MASK)) {
  342. dev_err(&client->dev, "ST2 status error 0x%x\n", ret);
  343. ret = -EINVAL;
  344. goto exit;
  345. }
  346. }
  347. /* Read the flux value from the appropriate register
  348. (the register is specified in the iio device attributes). */
  349. ret = i2c_smbus_read_word_data(client, ak8975_index_to_reg[index]);
  350. if (ret < 0) {
  351. dev_err(&client->dev, "Read axis data fails\n");
  352. goto exit;
  353. }
  354. meas_reg = ret;
  355. mutex_unlock(&data->lock);
  356. /* Endian conversion of the measured values. */
  357. raw = (s16) (le16_to_cpu(meas_reg));
  358. /* Clamp to valid range. */
  359. raw = clamp_t(s16, raw, -4096, 4095);
  360. *val = raw;
  361. return IIO_VAL_INT;
  362. exit:
  363. mutex_unlock(&data->lock);
  364. return ret;
  365. }
  366. static int ak8975_read_raw(struct iio_dev *indio_dev,
  367. struct iio_chan_spec const *chan,
  368. int *val, int *val2,
  369. long mask)
  370. {
  371. struct ak8975_data *data = iio_priv(indio_dev);
  372. switch (mask) {
  373. case IIO_CHAN_INFO_RAW:
  374. return ak8975_read_axis(indio_dev, chan->address, val);
  375. case IIO_CHAN_INFO_SCALE:
  376. *val = data->raw_to_gauss[chan->address];
  377. return IIO_VAL_INT;
  378. }
  379. return -EINVAL;
  380. }
  381. #define AK8975_CHANNEL(axis, index) \
  382. { \
  383. .type = IIO_MAGN, \
  384. .modified = 1, \
  385. .channel2 = IIO_MOD_##axis, \
  386. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  387. BIT(IIO_CHAN_INFO_SCALE), \
  388. .address = index, \
  389. }
  390. static const struct iio_chan_spec ak8975_channels[] = {
  391. AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2),
  392. };
  393. static const struct iio_info ak8975_info = {
  394. .read_raw = &ak8975_read_raw,
  395. .driver_module = THIS_MODULE,
  396. };
  397. static int ak8975_probe(struct i2c_client *client,
  398. const struct i2c_device_id *id)
  399. {
  400. struct ak8975_data *data;
  401. struct iio_dev *indio_dev;
  402. int eoc_gpio;
  403. int err;
  404. /* Grab and set up the supplied GPIO. */
  405. if (client->dev.platform_data)
  406. eoc_gpio = *(int *)(client->dev.platform_data);
  407. else if (client->dev.of_node)
  408. eoc_gpio = of_get_gpio(client->dev.of_node, 0);
  409. else
  410. eoc_gpio = -1;
  411. if (eoc_gpio == -EPROBE_DEFER)
  412. return -EPROBE_DEFER;
  413. /* We may not have a GPIO based IRQ to scan, that is fine, we will
  414. poll if so */
  415. if (gpio_is_valid(eoc_gpio)) {
  416. err = gpio_request_one(eoc_gpio, GPIOF_IN, "ak_8975");
  417. if (err < 0) {
  418. dev_err(&client->dev,
  419. "failed to request GPIO %d, error %d\n",
  420. eoc_gpio, err);
  421. goto exit;
  422. }
  423. }
  424. /* Register with IIO */
  425. indio_dev = iio_device_alloc(sizeof(*data));
  426. if (indio_dev == NULL) {
  427. err = -ENOMEM;
  428. goto exit_gpio;
  429. }
  430. data = iio_priv(indio_dev);
  431. i2c_set_clientdata(client, indio_dev);
  432. data->client = client;
  433. data->eoc_gpio = eoc_gpio;
  434. data->eoc_irq = 0;
  435. /* Perform some basic start-of-day setup of the device. */
  436. err = ak8975_setup(client);
  437. if (err < 0) {
  438. dev_err(&client->dev, "AK8975 initialization fails\n");
  439. goto exit_free_iio;
  440. }
  441. data->client = client;
  442. mutex_init(&data->lock);
  443. data->eoc_gpio = eoc_gpio;
  444. indio_dev->dev.parent = &client->dev;
  445. indio_dev->channels = ak8975_channels;
  446. indio_dev->num_channels = ARRAY_SIZE(ak8975_channels);
  447. indio_dev->info = &ak8975_info;
  448. indio_dev->modes = INDIO_DIRECT_MODE;
  449. err = iio_device_register(indio_dev);
  450. if (err < 0)
  451. goto exit_free_iio;
  452. return 0;
  453. exit_free_iio:
  454. iio_device_free(indio_dev);
  455. if (data->eoc_irq)
  456. free_irq(data->eoc_irq, data);
  457. exit_gpio:
  458. if (gpio_is_valid(eoc_gpio))
  459. gpio_free(eoc_gpio);
  460. exit:
  461. return err;
  462. }
  463. static int ak8975_remove(struct i2c_client *client)
  464. {
  465. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  466. struct ak8975_data *data = iio_priv(indio_dev);
  467. iio_device_unregister(indio_dev);
  468. if (data->eoc_irq)
  469. free_irq(data->eoc_irq, data);
  470. if (gpio_is_valid(data->eoc_gpio))
  471. gpio_free(data->eoc_gpio);
  472. iio_device_free(indio_dev);
  473. return 0;
  474. }
  475. static const struct i2c_device_id ak8975_id[] = {
  476. {"ak8975", 0},
  477. {}
  478. };
  479. MODULE_DEVICE_TABLE(i2c, ak8975_id);
  480. static const struct of_device_id ak8975_of_match[] = {
  481. { .compatible = "asahi-kasei,ak8975", },
  482. { .compatible = "ak8975", },
  483. { }
  484. };
  485. MODULE_DEVICE_TABLE(of, ak8975_of_match);
  486. static struct i2c_driver ak8975_driver = {
  487. .driver = {
  488. .name = "ak8975",
  489. .of_match_table = ak8975_of_match,
  490. },
  491. .probe = ak8975_probe,
  492. .remove = ak8975_remove,
  493. .id_table = ak8975_id,
  494. };
  495. module_i2c_driver(ak8975_driver);
  496. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  497. MODULE_DESCRIPTION("AK8975 magnetometer driver");
  498. MODULE_LICENSE("GPL");