ak09911.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * AK09911 3-axis compass driver
  3. * Copyright (c) 2014, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/types.h>
  18. #include <linux/slab.h>
  19. #include <linux/delay.h>
  20. #include <linux/i2c.h>
  21. #include <linux/acpi.h>
  22. #include <linux/iio/iio.h>
  23. #define AK09911_REG_WIA1 0x00
  24. #define AK09911_REG_WIA2 0x01
  25. #define AK09911_WIA1_VALUE 0x48
  26. #define AK09911_WIA2_VALUE 0x05
  27. #define AK09911_REG_ST1 0x10
  28. #define AK09911_REG_HXL 0x11
  29. #define AK09911_REG_HXH 0x12
  30. #define AK09911_REG_HYL 0x13
  31. #define AK09911_REG_HYH 0x14
  32. #define AK09911_REG_HZL 0x15
  33. #define AK09911_REG_HZH 0x16
  34. #define AK09911_REG_ASAX 0x60
  35. #define AK09911_REG_ASAY 0x61
  36. #define AK09911_REG_ASAZ 0x62
  37. #define AK09911_REG_CNTL1 0x30
  38. #define AK09911_REG_CNTL2 0x31
  39. #define AK09911_REG_CNTL3 0x32
  40. #define AK09911_MODE_SNG_MEASURE 0x01
  41. #define AK09911_MODE_SELF_TEST 0x10
  42. #define AK09911_MODE_FUSE_ACCESS 0x1F
  43. #define AK09911_MODE_POWERDOWN 0x00
  44. #define AK09911_RESET_DATA 0x01
  45. #define AK09911_REG_CNTL1 0x30
  46. #define AK09911_REG_CNTL2 0x31
  47. #define AK09911_REG_CNTL3 0x32
  48. #define AK09911_RAW_TO_GAUSS(asa) ((((asa) + 128) * 6000) / 256)
  49. #define AK09911_MAX_CONVERSION_TIMEOUT_MS 500
  50. #define AK09911_CONVERSION_DONE_POLL_TIME_MS 10
  51. struct ak09911_data {
  52. struct i2c_client *client;
  53. struct mutex lock;
  54. u8 asa[3];
  55. long raw_to_gauss[3];
  56. };
  57. static const int ak09911_index_to_reg[] = {
  58. AK09911_REG_HXL, AK09911_REG_HYL, AK09911_REG_HZL,
  59. };
  60. static int ak09911_set_mode(struct i2c_client *client, u8 mode)
  61. {
  62. int ret;
  63. switch (mode) {
  64. case AK09911_MODE_SNG_MEASURE:
  65. case AK09911_MODE_SELF_TEST:
  66. case AK09911_MODE_FUSE_ACCESS:
  67. case AK09911_MODE_POWERDOWN:
  68. ret = i2c_smbus_write_byte_data(client,
  69. AK09911_REG_CNTL2, mode);
  70. if (ret < 0) {
  71. dev_err(&client->dev, "set_mode error\n");
  72. return ret;
  73. }
  74. /* After mode change wait atleast 100us */
  75. usleep_range(100, 500);
  76. break;
  77. default:
  78. dev_err(&client->dev,
  79. "%s: Unknown mode(%d).", __func__, mode);
  80. return -EINVAL;
  81. }
  82. return ret;
  83. }
  84. /* Get Sensitivity Adjustment value */
  85. static int ak09911_get_asa(struct i2c_client *client)
  86. {
  87. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  88. struct ak09911_data *data = iio_priv(indio_dev);
  89. int ret;
  90. ret = ak09911_set_mode(client, AK09911_MODE_FUSE_ACCESS);
  91. if (ret < 0)
  92. return ret;
  93. /* Get asa data and store in the device data. */
  94. ret = i2c_smbus_read_i2c_block_data(client, AK09911_REG_ASAX,
  95. 3, data->asa);
  96. if (ret < 0) {
  97. dev_err(&client->dev, "Not able to read asa data\n");
  98. return ret;
  99. }
  100. ret = ak09911_set_mode(client, AK09911_MODE_POWERDOWN);
  101. if (ret < 0)
  102. return ret;
  103. data->raw_to_gauss[0] = AK09911_RAW_TO_GAUSS(data->asa[0]);
  104. data->raw_to_gauss[1] = AK09911_RAW_TO_GAUSS(data->asa[1]);
  105. data->raw_to_gauss[2] = AK09911_RAW_TO_GAUSS(data->asa[2]);
  106. return 0;
  107. }
  108. static int ak09911_verify_chip_id(struct i2c_client *client)
  109. {
  110. u8 wia_val[2];
  111. int ret;
  112. ret = i2c_smbus_read_i2c_block_data(client, AK09911_REG_WIA1,
  113. 2, wia_val);
  114. if (ret < 0) {
  115. dev_err(&client->dev, "Error reading WIA\n");
  116. return ret;
  117. }
  118. dev_dbg(&client->dev, "WIA %02x %02x\n", wia_val[0], wia_val[1]);
  119. if (wia_val[0] != AK09911_WIA1_VALUE ||
  120. wia_val[1] != AK09911_WIA2_VALUE) {
  121. dev_err(&client->dev, "Device ak09911 not found\n");
  122. return -ENODEV;
  123. }
  124. return 0;
  125. }
  126. static int wait_conversion_complete_polled(struct ak09911_data *data)
  127. {
  128. struct i2c_client *client = data->client;
  129. u8 read_status;
  130. u32 timeout_ms = AK09911_MAX_CONVERSION_TIMEOUT_MS;
  131. int ret;
  132. /* Wait for the conversion to complete. */
  133. while (timeout_ms) {
  134. msleep_interruptible(AK09911_CONVERSION_DONE_POLL_TIME_MS);
  135. ret = i2c_smbus_read_byte_data(client, AK09911_REG_ST1);
  136. if (ret < 0) {
  137. dev_err(&client->dev, "Error in reading ST1\n");
  138. return ret;
  139. }
  140. read_status = ret & 0x01;
  141. if (read_status)
  142. break;
  143. timeout_ms -= AK09911_CONVERSION_DONE_POLL_TIME_MS;
  144. }
  145. if (!timeout_ms) {
  146. dev_err(&client->dev, "Conversion timeout happened\n");
  147. return -EIO;
  148. }
  149. return read_status;
  150. }
  151. static int ak09911_read_axis(struct iio_dev *indio_dev, int index, int *val)
  152. {
  153. struct ak09911_data *data = iio_priv(indio_dev);
  154. struct i2c_client *client = data->client;
  155. int ret;
  156. mutex_lock(&data->lock);
  157. ret = ak09911_set_mode(client, AK09911_MODE_SNG_MEASURE);
  158. if (ret < 0)
  159. goto fn_exit;
  160. ret = wait_conversion_complete_polled(data);
  161. if (ret < 0)
  162. goto fn_exit;
  163. /* Read data */
  164. ret = i2c_smbus_read_word_data(client, ak09911_index_to_reg[index]);
  165. if (ret < 0) {
  166. dev_err(&client->dev, "Read axis data fails\n");
  167. goto fn_exit;
  168. }
  169. mutex_unlock(&data->lock);
  170. /* Clamp to valid range. */
  171. *val = sign_extend32(clamp_t(s16, ret, -8192, 8191), 13);
  172. return IIO_VAL_INT;
  173. fn_exit:
  174. mutex_unlock(&data->lock);
  175. return ret;
  176. }
  177. static int ak09911_read_raw(struct iio_dev *indio_dev,
  178. struct iio_chan_spec const *chan,
  179. int *val, int *val2,
  180. long mask)
  181. {
  182. struct ak09911_data *data = iio_priv(indio_dev);
  183. switch (mask) {
  184. case IIO_CHAN_INFO_RAW:
  185. return ak09911_read_axis(indio_dev, chan->address, val);
  186. case IIO_CHAN_INFO_SCALE:
  187. *val = 0;
  188. *val2 = data->raw_to_gauss[chan->address];
  189. return IIO_VAL_INT_PLUS_MICRO;
  190. }
  191. return -EINVAL;
  192. }
  193. #define AK09911_CHANNEL(axis, index) \
  194. { \
  195. .type = IIO_MAGN, \
  196. .modified = 1, \
  197. .channel2 = IIO_MOD_##axis, \
  198. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  199. BIT(IIO_CHAN_INFO_SCALE), \
  200. .address = index, \
  201. }
  202. static const struct iio_chan_spec ak09911_channels[] = {
  203. AK09911_CHANNEL(X, 0), AK09911_CHANNEL(Y, 1), AK09911_CHANNEL(Z, 2),
  204. };
  205. static const struct iio_info ak09911_info = {
  206. .read_raw = &ak09911_read_raw,
  207. .driver_module = THIS_MODULE,
  208. };
  209. static const struct acpi_device_id ak_acpi_match[] = {
  210. {"AK009911", 0},
  211. { },
  212. };
  213. MODULE_DEVICE_TABLE(acpi, ak_acpi_match);
  214. static int ak09911_probe(struct i2c_client *client,
  215. const struct i2c_device_id *id)
  216. {
  217. struct iio_dev *indio_dev;
  218. struct ak09911_data *data;
  219. const char *name;
  220. int ret;
  221. ret = ak09911_verify_chip_id(client);
  222. if (ret) {
  223. dev_err(&client->dev, "AK00911 not detected\n");
  224. return -ENODEV;
  225. }
  226. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  227. if (indio_dev == NULL)
  228. return -ENOMEM;
  229. data = iio_priv(indio_dev);
  230. i2c_set_clientdata(client, indio_dev);
  231. data->client = client;
  232. mutex_init(&data->lock);
  233. ret = ak09911_get_asa(client);
  234. if (ret)
  235. return ret;
  236. if (id)
  237. name = id->name;
  238. else if (ACPI_HANDLE(&client->dev))
  239. name = dev_name(&client->dev);
  240. else
  241. return -ENODEV;
  242. dev_dbg(&client->dev, "Asahi compass chip %s\n", name);
  243. indio_dev->dev.parent = &client->dev;
  244. indio_dev->channels = ak09911_channels;
  245. indio_dev->num_channels = ARRAY_SIZE(ak09911_channels);
  246. indio_dev->info = &ak09911_info;
  247. indio_dev->modes = INDIO_DIRECT_MODE;
  248. indio_dev->name = name;
  249. return devm_iio_device_register(&client->dev, indio_dev);
  250. }
  251. static const struct i2c_device_id ak09911_id[] = {
  252. {"ak09911", 0},
  253. {}
  254. };
  255. MODULE_DEVICE_TABLE(i2c, ak09911_id);
  256. static struct i2c_driver ak09911_driver = {
  257. .driver = {
  258. .name = "ak09911",
  259. .acpi_match_table = ACPI_PTR(ak_acpi_match),
  260. },
  261. .probe = ak09911_probe,
  262. .id_table = ak09911_id,
  263. };
  264. module_i2c_driver(ak09911_driver);
  265. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
  266. MODULE_LICENSE("GPL v2");
  267. MODULE_DESCRIPTION("AK09911 Compass driver");