ak8975.c 13 KB

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