stk8312.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /**
  2. * Sensortek STK8312 3-Axis Accelerometer
  3. *
  4. * Copyright (c) 2015, Intel Corporation.
  5. *
  6. * This file is subject to the terms and conditions of version 2 of
  7. * the GNU General Public License. See the file COPYING in the main
  8. * directory of this archive for more details.
  9. *
  10. * IIO driver for STK8312; 7-bit I2C address: 0x3D.
  11. */
  12. #include <linux/acpi.h>
  13. #include <linux/i2c.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/delay.h>
  17. #include <linux/iio/iio.h>
  18. #include <linux/iio/sysfs.h>
  19. #define STK8312_REG_XOUT 0x00
  20. #define STK8312_REG_YOUT 0x01
  21. #define STK8312_REG_ZOUT 0x02
  22. #define STK8312_REG_MODE 0x07
  23. #define STK8312_REG_STH 0x13
  24. #define STK8312_REG_RESET 0x20
  25. #define STK8312_REG_AFECTRL 0x24
  26. #define STK8312_REG_OTPADDR 0x3D
  27. #define STK8312_REG_OTPDATA 0x3E
  28. #define STK8312_REG_OTPCTRL 0x3F
  29. #define STK8312_MODE_ACTIVE 1
  30. #define STK8312_MODE_STANDBY 0
  31. #define STK8312_MODE_MASK 0x01
  32. #define STK8312_RNG_MASK 0xC0
  33. #define STK8312_RNG_SHIFT 6
  34. #define STK8312_READ_RETRIES 16
  35. #define STK8312_DRIVER_NAME "stk8312"
  36. /*
  37. * The accelerometer has two measurement ranges:
  38. *
  39. * -6g - +6g (8-bit, signed)
  40. * -16g - +16g (8-bit, signed)
  41. *
  42. * scale1 = (6 + 6) * 9.81 / (2^8 - 1) = 0.4616
  43. * scale2 = (16 + 16) * 9.81 / (2^8 - 1) = 1.2311
  44. */
  45. #define STK8312_SCALE_AVAIL "0.4616 1.2311"
  46. static const int stk8312_scale_table[][2] = {
  47. {0, 461600}, {1, 231100}
  48. };
  49. #define STK8312_ACCEL_CHANNEL(reg, axis) { \
  50. .type = IIO_ACCEL, \
  51. .address = reg, \
  52. .modified = 1, \
  53. .channel2 = IIO_MOD_##axis, \
  54. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  55. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  56. }
  57. static const struct iio_chan_spec stk8312_channels[] = {
  58. STK8312_ACCEL_CHANNEL(STK8312_REG_XOUT, X),
  59. STK8312_ACCEL_CHANNEL(STK8312_REG_YOUT, Y),
  60. STK8312_ACCEL_CHANNEL(STK8312_REG_ZOUT, Z),
  61. };
  62. struct stk8312_data {
  63. struct i2c_client *client;
  64. struct mutex lock;
  65. int range;
  66. u8 mode;
  67. };
  68. static IIO_CONST_ATTR(in_accel_scale_available, STK8312_SCALE_AVAIL);
  69. static struct attribute *stk8312_attributes[] = {
  70. &iio_const_attr_in_accel_scale_available.dev_attr.attr,
  71. NULL,
  72. };
  73. static const struct attribute_group stk8312_attribute_group = {
  74. .attrs = stk8312_attributes
  75. };
  76. static int stk8312_otp_init(struct stk8312_data *data)
  77. {
  78. int ret;
  79. int count = 10;
  80. struct i2c_client *client = data->client;
  81. ret = i2c_smbus_write_byte_data(client, STK8312_REG_OTPADDR, 0x70);
  82. if (ret < 0)
  83. goto exit_err;
  84. ret = i2c_smbus_write_byte_data(client, STK8312_REG_OTPCTRL, 0x02);
  85. if (ret < 0)
  86. goto exit_err;
  87. do {
  88. usleep_range(1000, 5000);
  89. ret = i2c_smbus_read_byte_data(client, STK8312_REG_OTPCTRL);
  90. if (ret < 0)
  91. goto exit_err;
  92. count--;
  93. } while (!(ret & 0x80) && count > 0);
  94. if (count == 0)
  95. goto exit_err;
  96. ret = i2c_smbus_read_byte_data(client, STK8312_REG_OTPDATA);
  97. if (ret < 0)
  98. goto exit_err;
  99. ret = i2c_smbus_write_byte_data(data->client,
  100. STK8312_REG_AFECTRL, ret);
  101. if (ret < 0)
  102. goto exit_err;
  103. msleep(150);
  104. return ret;
  105. exit_err:
  106. dev_err(&client->dev, "failed to initialize sensor\n");
  107. return ret;
  108. }
  109. static int stk8312_set_mode(struct stk8312_data *data, u8 mode)
  110. {
  111. int ret;
  112. u8 masked_reg;
  113. struct i2c_client *client = data->client;
  114. if (mode > 1)
  115. return -EINVAL;
  116. else if (mode == data->mode)
  117. return 0;
  118. ret = i2c_smbus_read_byte_data(client, STK8312_REG_MODE);
  119. if (ret < 0) {
  120. dev_err(&client->dev, "failed to change sensor mode\n");
  121. return ret;
  122. }
  123. masked_reg = ret & (~STK8312_MODE_MASK);
  124. masked_reg |= mode;
  125. ret = i2c_smbus_write_byte_data(client,
  126. STK8312_REG_MODE, masked_reg);
  127. if (ret < 0) {
  128. dev_err(&client->dev, "failed to change sensor mode\n");
  129. return ret;
  130. }
  131. data->mode = mode;
  132. if (mode == STK8312_MODE_ACTIVE) {
  133. /* Need to run OTP sequence before entering active mode */
  134. usleep_range(1000, 5000);
  135. ret = stk8312_otp_init(data);
  136. }
  137. return ret;
  138. }
  139. static int stk8312_set_range(struct stk8312_data *data, u8 range)
  140. {
  141. int ret;
  142. u8 masked_reg;
  143. u8 mode;
  144. struct i2c_client *client = data->client;
  145. if (range != 1 && range != 2)
  146. return -EINVAL;
  147. else if (range == data->range)
  148. return 0;
  149. mode = data->mode;
  150. /* We need to go in standby mode to modify registers */
  151. ret = stk8312_set_mode(data, STK8312_MODE_STANDBY);
  152. if (ret < 0)
  153. return ret;
  154. ret = i2c_smbus_read_byte_data(client, STK8312_REG_STH);
  155. if (ret < 0) {
  156. dev_err(&client->dev, "failed to change sensor range\n");
  157. return ret;
  158. }
  159. masked_reg = ret & (~STK8312_RNG_MASK);
  160. masked_reg |= range << STK8312_RNG_SHIFT;
  161. ret = i2c_smbus_write_byte_data(client, STK8312_REG_STH, masked_reg);
  162. if (ret < 0)
  163. dev_err(&client->dev, "failed to change sensor range\n");
  164. else
  165. data->range = range;
  166. return stk8312_set_mode(data, mode);
  167. }
  168. static int stk8312_read_accel(struct stk8312_data *data, u8 address)
  169. {
  170. int ret;
  171. struct i2c_client *client = data->client;
  172. if (address > 2)
  173. return -EINVAL;
  174. ret = i2c_smbus_read_byte_data(client, address);
  175. if (ret < 0) {
  176. dev_err(&client->dev, "register read failed\n");
  177. return ret;
  178. }
  179. return sign_extend32(ret, 7);
  180. }
  181. static int stk8312_read_raw(struct iio_dev *indio_dev,
  182. struct iio_chan_spec const *chan,
  183. int *val, int *val2, long mask)
  184. {
  185. struct stk8312_data *data = iio_priv(indio_dev);
  186. if (chan->type != IIO_ACCEL)
  187. return -EINVAL;
  188. switch (mask) {
  189. case IIO_CHAN_INFO_RAW:
  190. mutex_lock(&data->lock);
  191. *val = stk8312_read_accel(data, chan->address);
  192. mutex_unlock(&data->lock);
  193. return IIO_VAL_INT;
  194. case IIO_CHAN_INFO_SCALE:
  195. *val = stk8312_scale_table[data->range - 1][0];
  196. *val2 = stk8312_scale_table[data->range - 1][1];
  197. return IIO_VAL_INT_PLUS_MICRO;
  198. }
  199. return -EINVAL;
  200. }
  201. static int stk8312_write_raw(struct iio_dev *indio_dev,
  202. struct iio_chan_spec const *chan,
  203. int val, int val2, long mask)
  204. {
  205. int i;
  206. int index = -1;
  207. int ret;
  208. struct stk8312_data *data = iio_priv(indio_dev);
  209. switch (mask) {
  210. case IIO_CHAN_INFO_SCALE:
  211. for (i = 0; i < ARRAY_SIZE(stk8312_scale_table); i++)
  212. if (val == stk8312_scale_table[i][0] &&
  213. val2 == stk8312_scale_table[i][1]) {
  214. index = i + 1;
  215. break;
  216. }
  217. if (index < 0)
  218. return -EINVAL;
  219. mutex_lock(&data->lock);
  220. ret = stk8312_set_range(data, index);
  221. mutex_unlock(&data->lock);
  222. return ret;
  223. }
  224. return -EINVAL;
  225. }
  226. static const struct iio_info stk8312_info = {
  227. .driver_module = THIS_MODULE,
  228. .read_raw = stk8312_read_raw,
  229. .write_raw = stk8312_write_raw,
  230. .attrs = &stk8312_attribute_group,
  231. };
  232. static int stk8312_probe(struct i2c_client *client,
  233. const struct i2c_device_id *id)
  234. {
  235. int ret;
  236. struct iio_dev *indio_dev;
  237. struct stk8312_data *data;
  238. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  239. if (!indio_dev) {
  240. dev_err(&client->dev, "iio allocation failed!\n");
  241. return -ENOMEM;
  242. }
  243. data = iio_priv(indio_dev);
  244. data->client = client;
  245. i2c_set_clientdata(client, indio_dev);
  246. mutex_init(&data->lock);
  247. indio_dev->dev.parent = &client->dev;
  248. indio_dev->info = &stk8312_info;
  249. indio_dev->name = STK8312_DRIVER_NAME;
  250. indio_dev->modes = INDIO_DIRECT_MODE;
  251. indio_dev->channels = stk8312_channels;
  252. indio_dev->num_channels = ARRAY_SIZE(stk8312_channels);
  253. /* A software reset is recommended at power-on */
  254. ret = i2c_smbus_write_byte_data(data->client, STK8312_REG_RESET, 0x00);
  255. if (ret < 0) {
  256. dev_err(&client->dev, "failed to reset sensor\n");
  257. return ret;
  258. }
  259. ret = stk8312_set_range(data, 1);
  260. if (ret < 0)
  261. return ret;
  262. ret = stk8312_set_mode(data, STK8312_MODE_ACTIVE);
  263. if (ret < 0)
  264. return ret;
  265. ret = iio_device_register(indio_dev);
  266. if (ret < 0) {
  267. dev_err(&client->dev, "device_register failed\n");
  268. stk8312_set_mode(data, STK8312_MODE_STANDBY);
  269. }
  270. return ret;
  271. }
  272. static int stk8312_remove(struct i2c_client *client)
  273. {
  274. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  275. iio_device_unregister(indio_dev);
  276. return stk8312_set_mode(iio_priv(indio_dev), STK8312_MODE_STANDBY);
  277. }
  278. #ifdef CONFIG_PM_SLEEP
  279. static int stk8312_suspend(struct device *dev)
  280. {
  281. struct stk8312_data *data;
  282. data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
  283. return stk8312_set_mode(data, STK8312_MODE_STANDBY);
  284. }
  285. static int stk8312_resume(struct device *dev)
  286. {
  287. struct stk8312_data *data;
  288. data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
  289. return stk8312_set_mode(data, STK8312_MODE_ACTIVE);
  290. }
  291. static SIMPLE_DEV_PM_OPS(stk8312_pm_ops, stk8312_suspend, stk8312_resume);
  292. #define STK8312_PM_OPS (&stk8312_pm_ops)
  293. #else
  294. #define STK8312_PM_OPS NULL
  295. #endif
  296. static const struct i2c_device_id stk8312_i2c_id[] = {
  297. {"STK8312", 0},
  298. {}
  299. };
  300. static const struct acpi_device_id stk8312_acpi_id[] = {
  301. {"STK8312", 0},
  302. {}
  303. };
  304. MODULE_DEVICE_TABLE(acpi, stk8312_acpi_id);
  305. static struct i2c_driver stk8312_driver = {
  306. .driver = {
  307. .name = "stk8312",
  308. .pm = STK8312_PM_OPS,
  309. .acpi_match_table = ACPI_PTR(stk8312_acpi_id),
  310. },
  311. .probe = stk8312_probe,
  312. .remove = stk8312_remove,
  313. .id_table = stk8312_i2c_id,
  314. };
  315. module_i2c_driver(stk8312_driver);
  316. MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
  317. MODULE_DESCRIPTION("STK8312 3-Axis Accelerometer driver");
  318. MODULE_LICENSE("GPL v2");