ak8975.c 12 KB

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