ad7152.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*
  2. * AD7152 capacitive sensor driver supporting AD7152/3
  3. *
  4. * Copyright 2010-2011a Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later.
  7. */
  8. #include <linux/interrupt.h>
  9. #include <linux/device.h>
  10. #include <linux/kernel.h>
  11. #include <linux/slab.h>
  12. #include <linux/sysfs.h>
  13. #include <linux/i2c.h>
  14. #include <linux/module.h>
  15. #include <linux/delay.h>
  16. #include <linux/iio/iio.h>
  17. #include <linux/iio/sysfs.h>
  18. /*
  19. * TODO: Check compliance of calibbias with abi (units)
  20. */
  21. /*
  22. * AD7152 registers definition
  23. */
  24. #define AD7152_REG_STATUS 0
  25. #define AD7152_REG_CH1_DATA_HIGH 1
  26. #define AD7152_REG_CH2_DATA_HIGH 3
  27. #define AD7152_REG_CH1_OFFS_HIGH 5
  28. #define AD7152_REG_CH2_OFFS_HIGH 7
  29. #define AD7152_REG_CH1_GAIN_HIGH 9
  30. #define AD7152_REG_CH1_SETUP 11
  31. #define AD7152_REG_CH2_GAIN_HIGH 12
  32. #define AD7152_REG_CH2_SETUP 14
  33. #define AD7152_REG_CFG 15
  34. #define AD7152_REG_RESEVERD 16
  35. #define AD7152_REG_CAPDAC_POS 17
  36. #define AD7152_REG_CAPDAC_NEG 18
  37. #define AD7152_REG_CFG2 26
  38. /* Status Register Bit Designations (AD7152_REG_STATUS) */
  39. #define AD7152_STATUS_RDY1 (1 << 0)
  40. #define AD7152_STATUS_RDY2 (1 << 1)
  41. #define AD7152_STATUS_C1C2 (1 << 2)
  42. #define AD7152_STATUS_PWDN (1 << 7)
  43. /* Setup Register Bit Designations (AD7152_REG_CHx_SETUP) */
  44. #define AD7152_SETUP_CAPDIFF (1 << 5)
  45. #define AD7152_SETUP_RANGE_2pF (0 << 6)
  46. #define AD7152_SETUP_RANGE_0_5pF (1 << 6)
  47. #define AD7152_SETUP_RANGE_1pF (2 << 6)
  48. #define AD7152_SETUP_RANGE_4pF (3 << 6)
  49. #define AD7152_SETUP_RANGE(x) ((x) << 6)
  50. /* Config Register Bit Designations (AD7152_REG_CFG) */
  51. #define AD7152_CONF_CH2EN (1 << 3)
  52. #define AD7152_CONF_CH1EN (1 << 4)
  53. #define AD7152_CONF_MODE_IDLE (0 << 0)
  54. #define AD7152_CONF_MODE_CONT_CONV (1 << 0)
  55. #define AD7152_CONF_MODE_SINGLE_CONV (2 << 0)
  56. #define AD7152_CONF_MODE_OFFS_CAL (5 << 0)
  57. #define AD7152_CONF_MODE_GAIN_CAL (6 << 0)
  58. /* Capdac Register Bit Designations (AD7152_REG_CAPDAC_XXX) */
  59. #define AD7152_CAPDAC_DACEN (1 << 7)
  60. #define AD7152_CAPDAC_DACP(x) ((x) & 0x1F)
  61. /* CFG2 Register Bit Designations (AD7152_REG_CFG2) */
  62. #define AD7152_CFG2_OSR(x) (((x) & 0x3) << 4)
  63. enum {
  64. AD7152_DATA,
  65. AD7152_OFFS,
  66. AD7152_GAIN,
  67. AD7152_SETUP
  68. };
  69. /*
  70. * struct ad7152_chip_info - chip specific information
  71. */
  72. struct ad7152_chip_info {
  73. struct i2c_client *client;
  74. /*
  75. * Capacitive channel digital filter setup;
  76. * conversion time/update rate setup per channel
  77. */
  78. u8 filter_rate_setup;
  79. u8 setup[2];
  80. };
  81. static inline ssize_t ad7152_start_calib(struct device *dev,
  82. struct device_attribute *attr,
  83. const char *buf,
  84. size_t len,
  85. u8 regval)
  86. {
  87. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  88. struct ad7152_chip_info *chip = iio_priv(indio_dev);
  89. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  90. bool doit;
  91. int ret, timeout = 10;
  92. ret = strtobool(buf, &doit);
  93. if (ret < 0)
  94. return ret;
  95. if (!doit)
  96. return 0;
  97. if (this_attr->address == 0)
  98. regval |= AD7152_CONF_CH1EN;
  99. else
  100. regval |= AD7152_CONF_CH2EN;
  101. mutex_lock(&indio_dev->mlock);
  102. ret = i2c_smbus_write_byte_data(chip->client, AD7152_REG_CFG, regval);
  103. if (ret < 0) {
  104. mutex_unlock(&indio_dev->mlock);
  105. return ret;
  106. }
  107. do {
  108. mdelay(20);
  109. ret = i2c_smbus_read_byte_data(chip->client, AD7152_REG_CFG);
  110. if (ret < 0) {
  111. mutex_unlock(&indio_dev->mlock);
  112. return ret;
  113. }
  114. } while ((ret == regval) && timeout--);
  115. mutex_unlock(&indio_dev->mlock);
  116. return len;
  117. }
  118. static ssize_t ad7152_start_offset_calib(struct device *dev,
  119. struct device_attribute *attr,
  120. const char *buf,
  121. size_t len)
  122. {
  123. return ad7152_start_calib(dev, attr, buf, len,
  124. AD7152_CONF_MODE_OFFS_CAL);
  125. }
  126. static ssize_t ad7152_start_gain_calib(struct device *dev,
  127. struct device_attribute *attr,
  128. const char *buf,
  129. size_t len)
  130. {
  131. return ad7152_start_calib(dev, attr, buf, len,
  132. AD7152_CONF_MODE_GAIN_CAL);
  133. }
  134. static IIO_DEVICE_ATTR(in_capacitance0_calibbias_calibration,
  135. S_IWUSR, NULL, ad7152_start_offset_calib, 0);
  136. static IIO_DEVICE_ATTR(in_capacitance1_calibbias_calibration,
  137. S_IWUSR, NULL, ad7152_start_offset_calib, 1);
  138. static IIO_DEVICE_ATTR(in_capacitance0_calibscale_calibration,
  139. S_IWUSR, NULL, ad7152_start_gain_calib, 0);
  140. static IIO_DEVICE_ATTR(in_capacitance1_calibscale_calibration,
  141. S_IWUSR, NULL, ad7152_start_gain_calib, 1);
  142. /* Values are Update Rate (Hz), Conversion Time (ms) + 1*/
  143. static const unsigned char ad7152_filter_rate_table[][2] = {
  144. {200, 5 + 1}, {50, 20 + 1}, {20, 50 + 1}, {17, 60 + 1},
  145. };
  146. static ssize_t ad7152_show_filter_rate_setup(struct device *dev,
  147. struct device_attribute *attr,
  148. char *buf)
  149. {
  150. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  151. struct ad7152_chip_info *chip = iio_priv(indio_dev);
  152. return sprintf(buf, "%d\n",
  153. ad7152_filter_rate_table[chip->filter_rate_setup][0]);
  154. }
  155. static ssize_t ad7152_store_filter_rate_setup(struct device *dev,
  156. struct device_attribute *attr,
  157. const char *buf,
  158. size_t len)
  159. {
  160. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  161. struct ad7152_chip_info *chip = iio_priv(indio_dev);
  162. u8 data;
  163. int ret, i;
  164. ret = kstrtou8(buf, 10, &data);
  165. if (ret < 0)
  166. return ret;
  167. for (i = 0; i < ARRAY_SIZE(ad7152_filter_rate_table); i++)
  168. if (data >= ad7152_filter_rate_table[i][0])
  169. break;
  170. if (i >= ARRAY_SIZE(ad7152_filter_rate_table))
  171. i = ARRAY_SIZE(ad7152_filter_rate_table) - 1;
  172. mutex_lock(&indio_dev->mlock);
  173. ret = i2c_smbus_write_byte_data(chip->client,
  174. AD7152_REG_CFG2, AD7152_CFG2_OSR(i));
  175. if (ret < 0) {
  176. mutex_unlock(&indio_dev->mlock);
  177. return ret;
  178. }
  179. chip->filter_rate_setup = i;
  180. mutex_unlock(&indio_dev->mlock);
  181. return len;
  182. }
  183. static IIO_DEV_ATTR_SAMP_FREQ(S_IRUGO | S_IWUSR,
  184. ad7152_show_filter_rate_setup,
  185. ad7152_store_filter_rate_setup);
  186. static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("200 50 20 17");
  187. static IIO_CONST_ATTR(in_capacitance_scale_available,
  188. "0.000061050 0.000030525 0.000015263 0.000007631");
  189. static struct attribute *ad7152_attributes[] = {
  190. &iio_dev_attr_sampling_frequency.dev_attr.attr,
  191. &iio_dev_attr_in_capacitance0_calibbias_calibration.dev_attr.attr,
  192. &iio_dev_attr_in_capacitance1_calibbias_calibration.dev_attr.attr,
  193. &iio_dev_attr_in_capacitance0_calibscale_calibration.dev_attr.attr,
  194. &iio_dev_attr_in_capacitance1_calibscale_calibration.dev_attr.attr,
  195. &iio_const_attr_in_capacitance_scale_available.dev_attr.attr,
  196. &iio_const_attr_sampling_frequency_available.dev_attr.attr,
  197. NULL,
  198. };
  199. static const struct attribute_group ad7152_attribute_group = {
  200. .attrs = ad7152_attributes,
  201. };
  202. static const u8 ad7152_addresses[][4] = {
  203. { AD7152_REG_CH1_DATA_HIGH, AD7152_REG_CH1_OFFS_HIGH,
  204. AD7152_REG_CH1_GAIN_HIGH, AD7152_REG_CH1_SETUP },
  205. { AD7152_REG_CH2_DATA_HIGH, AD7152_REG_CH2_OFFS_HIGH,
  206. AD7152_REG_CH2_GAIN_HIGH, AD7152_REG_CH2_SETUP },
  207. };
  208. /* Values are nano relative to pf base. */
  209. static const int ad7152_scale_table[] = {
  210. 30525, 7631, 15263, 61050
  211. };
  212. static int ad7152_write_raw(struct iio_dev *indio_dev,
  213. struct iio_chan_spec const *chan,
  214. int val,
  215. int val2,
  216. long mask)
  217. {
  218. struct ad7152_chip_info *chip = iio_priv(indio_dev);
  219. int ret, i;
  220. mutex_lock(&indio_dev->mlock);
  221. switch (mask) {
  222. case IIO_CHAN_INFO_CALIBSCALE:
  223. if (val != 1) {
  224. ret = -EINVAL;
  225. goto out;
  226. }
  227. val = (val2 * 1024) / 15625;
  228. ret = i2c_smbus_write_word_data(chip->client,
  229. ad7152_addresses[chan->channel][AD7152_GAIN],
  230. swab16(val));
  231. if (ret < 0)
  232. goto out;
  233. ret = 0;
  234. break;
  235. case IIO_CHAN_INFO_CALIBBIAS:
  236. if ((val < 0) | (val > 0xFFFF)) {
  237. ret = -EINVAL;
  238. goto out;
  239. }
  240. ret = i2c_smbus_write_word_data(chip->client,
  241. ad7152_addresses[chan->channel][AD7152_OFFS],
  242. swab16(val));
  243. if (ret < 0)
  244. goto out;
  245. ret = 0;
  246. break;
  247. case IIO_CHAN_INFO_SCALE:
  248. if (val) {
  249. ret = -EINVAL;
  250. goto out;
  251. }
  252. for (i = 0; i < ARRAY_SIZE(ad7152_scale_table); i++)
  253. if (val2 == ad7152_scale_table[i])
  254. break;
  255. chip->setup[chan->channel] &= ~AD7152_SETUP_RANGE_4pF;
  256. chip->setup[chan->channel] |= AD7152_SETUP_RANGE(i);
  257. ret = i2c_smbus_write_byte_data(chip->client,
  258. ad7152_addresses[chan->channel][AD7152_SETUP],
  259. chip->setup[chan->channel]);
  260. if (ret < 0)
  261. goto out;
  262. ret = 0;
  263. break;
  264. default:
  265. ret = -EINVAL;
  266. }
  267. out:
  268. mutex_unlock(&indio_dev->mlock);
  269. return ret;
  270. }
  271. static int ad7152_read_raw(struct iio_dev *indio_dev,
  272. struct iio_chan_spec const *chan,
  273. int *val, int *val2,
  274. long mask)
  275. {
  276. struct ad7152_chip_info *chip = iio_priv(indio_dev);
  277. int ret;
  278. u8 regval = 0;
  279. mutex_lock(&indio_dev->mlock);
  280. switch (mask) {
  281. case IIO_CHAN_INFO_RAW:
  282. /* First set whether in differential mode */
  283. regval = chip->setup[chan->channel];
  284. if (chan->differential)
  285. chip->setup[chan->channel] |= AD7152_SETUP_CAPDIFF;
  286. else
  287. chip->setup[chan->channel] &= ~AD7152_SETUP_CAPDIFF;
  288. if (regval != chip->setup[chan->channel]) {
  289. ret = i2c_smbus_write_byte_data(chip->client,
  290. ad7152_addresses[chan->channel][AD7152_SETUP],
  291. chip->setup[chan->channel]);
  292. if (ret < 0)
  293. goto out;
  294. }
  295. /* Make sure the channel is enabled */
  296. if (chan->channel == 0)
  297. regval = AD7152_CONF_CH1EN;
  298. else
  299. regval = AD7152_CONF_CH2EN;
  300. /* Trigger a single read */
  301. regval |= AD7152_CONF_MODE_SINGLE_CONV;
  302. ret = i2c_smbus_write_byte_data(chip->client, AD7152_REG_CFG,
  303. regval);
  304. if (ret < 0)
  305. goto out;
  306. msleep(ad7152_filter_rate_table[chip->filter_rate_setup][1]);
  307. /* Now read the actual register */
  308. ret = i2c_smbus_read_word_data(chip->client,
  309. ad7152_addresses[chan->channel][AD7152_DATA]);
  310. if (ret < 0)
  311. goto out;
  312. *val = swab16(ret);
  313. if (chan->differential)
  314. *val -= 0x8000;
  315. ret = IIO_VAL_INT;
  316. break;
  317. case IIO_CHAN_INFO_CALIBSCALE:
  318. ret = i2c_smbus_read_word_data(chip->client,
  319. ad7152_addresses[chan->channel][AD7152_GAIN]);
  320. if (ret < 0)
  321. goto out;
  322. /* 1 + gain_val / 2^16 */
  323. *val = 1;
  324. *val2 = (15625 * swab16(ret)) / 1024;
  325. ret = IIO_VAL_INT_PLUS_MICRO;
  326. break;
  327. case IIO_CHAN_INFO_CALIBBIAS:
  328. ret = i2c_smbus_read_word_data(chip->client,
  329. ad7152_addresses[chan->channel][AD7152_OFFS]);
  330. if (ret < 0)
  331. goto out;
  332. *val = swab16(ret);
  333. ret = IIO_VAL_INT;
  334. break;
  335. case IIO_CHAN_INFO_SCALE:
  336. ret = i2c_smbus_read_byte_data(chip->client,
  337. ad7152_addresses[chan->channel][AD7152_SETUP]);
  338. if (ret < 0)
  339. goto out;
  340. *val = 0;
  341. *val2 = ad7152_scale_table[ret >> 6];
  342. ret = IIO_VAL_INT_PLUS_NANO;
  343. break;
  344. default:
  345. ret = -EINVAL;
  346. }
  347. out:
  348. mutex_unlock(&indio_dev->mlock);
  349. return ret;
  350. }
  351. static int ad7152_write_raw_get_fmt(struct iio_dev *indio_dev,
  352. struct iio_chan_spec const *chan,
  353. long mask)
  354. {
  355. switch (mask) {
  356. case IIO_CHAN_INFO_SCALE:
  357. return IIO_VAL_INT_PLUS_NANO;
  358. default:
  359. return IIO_VAL_INT_PLUS_MICRO;
  360. }
  361. }
  362. static const struct iio_info ad7152_info = {
  363. .attrs = &ad7152_attribute_group,
  364. .read_raw = &ad7152_read_raw,
  365. .write_raw = &ad7152_write_raw,
  366. .write_raw_get_fmt = &ad7152_write_raw_get_fmt,
  367. .driver_module = THIS_MODULE,
  368. };
  369. static const struct iio_chan_spec ad7152_channels[] = {
  370. {
  371. .type = IIO_CAPACITANCE,
  372. .indexed = 1,
  373. .channel = 0,
  374. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  375. BIT(IIO_CHAN_INFO_CALIBSCALE) |
  376. BIT(IIO_CHAN_INFO_CALIBBIAS) |
  377. BIT(IIO_CHAN_INFO_SCALE),
  378. }, {
  379. .type = IIO_CAPACITANCE,
  380. .differential = 1,
  381. .indexed = 1,
  382. .channel = 0,
  383. .channel2 = 2,
  384. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  385. BIT(IIO_CHAN_INFO_CALIBSCALE) |
  386. BIT(IIO_CHAN_INFO_CALIBBIAS) |
  387. BIT(IIO_CHAN_INFO_SCALE),
  388. }, {
  389. .type = IIO_CAPACITANCE,
  390. .indexed = 1,
  391. .channel = 1,
  392. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  393. BIT(IIO_CHAN_INFO_CALIBSCALE) |
  394. BIT(IIO_CHAN_INFO_CALIBBIAS) |
  395. BIT(IIO_CHAN_INFO_SCALE),
  396. }, {
  397. .type = IIO_CAPACITANCE,
  398. .differential = 1,
  399. .indexed = 1,
  400. .channel = 1,
  401. .channel2 = 3,
  402. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  403. BIT(IIO_CHAN_INFO_CALIBSCALE) |
  404. BIT(IIO_CHAN_INFO_CALIBBIAS) |
  405. BIT(IIO_CHAN_INFO_SCALE),
  406. }
  407. };
  408. /*
  409. * device probe and remove
  410. */
  411. static int ad7152_probe(struct i2c_client *client,
  412. const struct i2c_device_id *id)
  413. {
  414. int ret = 0;
  415. struct ad7152_chip_info *chip;
  416. struct iio_dev *indio_dev;
  417. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
  418. if (!indio_dev)
  419. return -ENOMEM;
  420. chip = iio_priv(indio_dev);
  421. /* this is only used for device removal purposes */
  422. i2c_set_clientdata(client, indio_dev);
  423. chip->client = client;
  424. /* Establish that the iio_dev is a child of the i2c device */
  425. indio_dev->name = id->name;
  426. indio_dev->dev.parent = &client->dev;
  427. indio_dev->info = &ad7152_info;
  428. indio_dev->channels = ad7152_channels;
  429. if (id->driver_data == 0)
  430. indio_dev->num_channels = ARRAY_SIZE(ad7152_channels);
  431. else
  432. indio_dev->num_channels = 2;
  433. indio_dev->num_channels = ARRAY_SIZE(ad7152_channels);
  434. indio_dev->modes = INDIO_DIRECT_MODE;
  435. ret = devm_iio_device_register(indio_dev->dev.parent, indio_dev);
  436. if (ret)
  437. return ret;
  438. dev_err(&client->dev, "%s capacitive sensor registered\n", id->name);
  439. return 0;
  440. }
  441. static const struct i2c_device_id ad7152_id[] = {
  442. { "ad7152", 0 },
  443. { "ad7153", 1 },
  444. {}
  445. };
  446. MODULE_DEVICE_TABLE(i2c, ad7152_id);
  447. static struct i2c_driver ad7152_driver = {
  448. .driver = {
  449. .name = KBUILD_MODNAME,
  450. },
  451. .probe = ad7152_probe,
  452. .id_table = ad7152_id,
  453. };
  454. module_i2c_driver(ad7152_driver);
  455. MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
  456. MODULE_DESCRIPTION("Analog Devices AD7152/3 capacitive sensor driver");
  457. MODULE_LICENSE("GPL v2");