/drivers/staging/iio/adc/ad7291.c

https://bitbucket.org/wisechild/galaxy-nexus · C · 907 lines · 722 code · 145 blank · 40 comment · 72 complexity · 1ba0afc78ab808aa0b639e09a559038f MD5 · raw file

  1. /*
  2. * AD7291 digital temperature sensor driver supporting AD7291
  3. *
  4. * Copyright 2010 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later.
  7. */
  8. #include <linux/interrupt.h>
  9. #include <linux/gpio.h>
  10. #include <linux/device.h>
  11. #include <linux/kernel.h>
  12. #include <linux/slab.h>
  13. #include <linux/sysfs.h>
  14. #include <linux/list.h>
  15. #include <linux/i2c.h>
  16. #include "../iio.h"
  17. #include "../sysfs.h"
  18. /*
  19. * AD7291 registers definition
  20. */
  21. #define AD7291_COMMAND 0
  22. #define AD7291_VOLTAGE 1
  23. #define AD7291_T_SENSE 2
  24. #define AD7291_T_AVERAGE 3
  25. #define AD7291_VOLTAGE_LIMIT_BASE 4
  26. #define AD7291_VOLTAGE_LIMIT_COUNT 8
  27. #define AD7291_T_SENSE_HIGH 0x1c
  28. #define AD7291_T_SENSE_LOW 0x1d
  29. #define AD7291_T_SENSE_HYST 0x1e
  30. #define AD7291_VOLTAGE_ALERT_STATUS 0x1f
  31. #define AD7291_T_ALERT_STATUS 0x20
  32. /*
  33. * AD7291 command
  34. */
  35. #define AD7291_AUTOCYCLE 0x1
  36. #define AD7291_RESET 0x2
  37. #define AD7291_ALART_CLEAR 0x4
  38. #define AD7291_ALART_POLARITY 0x8
  39. #define AD7291_EXT_REF 0x10
  40. #define AD7291_NOISE_DELAY 0x20
  41. #define AD7291_T_SENSE_MASK 0x40
  42. #define AD7291_VOLTAGE_MASK 0xff00
  43. #define AD7291_VOLTAGE_OFFSET 0x8
  44. /*
  45. * AD7291 value masks
  46. */
  47. #define AD7291_CHANNEL_MASK 0xf000
  48. #define AD7291_VALUE_MASK 0xfff
  49. #define AD7291_T_VALUE_SIGN 0x400
  50. #define AD7291_T_VALUE_FLOAT_OFFSET 2
  51. #define AD7291_T_VALUE_FLOAT_MASK 0x2
  52. /*
  53. * struct ad7291_chip_info - chip specifc information
  54. */
  55. struct ad7291_chip_info {
  56. struct i2c_client *client;
  57. struct iio_dev *indio_dev;
  58. u16 command;
  59. u8 channels; /* Active voltage channels */
  60. };
  61. /*
  62. * struct ad7291_chip_info - chip specifc information
  63. */
  64. struct ad7291_limit_regs {
  65. u16 data_high;
  66. u16 data_low;
  67. u16 hysteresis;
  68. };
  69. /*
  70. * ad7291 register access by I2C
  71. */
  72. static int ad7291_i2c_read(struct ad7291_chip_info *chip, u8 reg, u16 *data)
  73. {
  74. struct i2c_client *client = chip->client;
  75. int ret = 0;
  76. ret = i2c_smbus_read_word_data(client, reg);
  77. if (ret < 0) {
  78. dev_err(&client->dev, "I2C read error\n");
  79. return ret;
  80. }
  81. *data = swab16((u16)ret);
  82. return 0;
  83. }
  84. static int ad7291_i2c_write(struct ad7291_chip_info *chip, u8 reg, u16 data)
  85. {
  86. struct i2c_client *client = chip->client;
  87. int ret = 0;
  88. ret = i2c_smbus_write_word_data(client, reg, swab16(data));
  89. if (ret < 0)
  90. dev_err(&client->dev, "I2C write error\n");
  91. return ret;
  92. }
  93. /* Returns negative errno, or else the number of words read. */
  94. static int ad7291_i2c_read_data(struct ad7291_chip_info *chip, u8 reg, u16 *data)
  95. {
  96. struct i2c_client *client = chip->client;
  97. u8 commands[4];
  98. int ret = 0;
  99. int i, count;
  100. if (reg == AD7291_T_SENSE || reg == AD7291_T_AVERAGE)
  101. count = 2;
  102. else if (reg == AD7291_VOLTAGE) {
  103. if (!chip->channels) {
  104. dev_err(&client->dev, "No voltage channel is selected.\n");
  105. return -EINVAL;
  106. }
  107. count = 2 + chip->channels * 2;
  108. } else {
  109. dev_err(&client->dev, "I2C wrong data register\n");
  110. return -EINVAL;
  111. }
  112. commands[0] = 0;
  113. commands[1] = (chip->command >> 8) & 0xff;
  114. commands[2] = chip->command & 0xff;
  115. commands[3] = reg;
  116. ret = i2c_master_send(client, commands, 4);
  117. if (ret < 0) {
  118. dev_err(&client->dev, "I2C master send error\n");
  119. return ret;
  120. }
  121. ret = i2c_master_recv(client, (u8 *)data, count);
  122. if (ret < 0) {
  123. dev_err(&client->dev, "I2C master receive error\n");
  124. return ret;
  125. }
  126. ret >>= 2;
  127. for (i = 0; i < ret; i++)
  128. data[i] = swab16(data[i]);
  129. return ret;
  130. }
  131. static ssize_t ad7291_show_mode(struct device *dev,
  132. struct device_attribute *attr,
  133. char *buf)
  134. {
  135. struct iio_dev *dev_info = dev_get_drvdata(dev);
  136. struct ad7291_chip_info *chip = dev_info->dev_data;
  137. if (chip->command & AD7291_AUTOCYCLE)
  138. return sprintf(buf, "autocycle\n");
  139. else
  140. return sprintf(buf, "command\n");
  141. }
  142. static ssize_t ad7291_store_mode(struct device *dev,
  143. struct device_attribute *attr,
  144. const char *buf,
  145. size_t len)
  146. {
  147. struct iio_dev *dev_info = dev_get_drvdata(dev);
  148. struct ad7291_chip_info *chip = dev_info->dev_data;
  149. u16 command;
  150. int ret;
  151. command = chip->command & (~AD7291_AUTOCYCLE);
  152. if (strcmp(buf, "autocycle"))
  153. command |= AD7291_AUTOCYCLE;
  154. ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
  155. if (ret)
  156. return -EIO;
  157. chip->command = command;
  158. return ret;
  159. }
  160. static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
  161. ad7291_show_mode,
  162. ad7291_store_mode,
  163. 0);
  164. static ssize_t ad7291_show_available_modes(struct device *dev,
  165. struct device_attribute *attr,
  166. char *buf)
  167. {
  168. return sprintf(buf, "command\nautocycle\n");
  169. }
  170. static IIO_DEVICE_ATTR(available_modes, S_IRUGO, ad7291_show_available_modes, NULL, 0);
  171. static ssize_t ad7291_store_reset(struct device *dev,
  172. struct device_attribute *attr,
  173. const char *buf,
  174. size_t len)
  175. {
  176. struct iio_dev *dev_info = dev_get_drvdata(dev);
  177. struct ad7291_chip_info *chip = dev_info->dev_data;
  178. u16 command;
  179. int ret;
  180. command = chip->command | AD7291_RESET;
  181. ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
  182. if (ret)
  183. return -EIO;
  184. return ret;
  185. }
  186. static IIO_DEVICE_ATTR(reset, S_IWUSR,
  187. NULL,
  188. ad7291_store_reset,
  189. 0);
  190. static ssize_t ad7291_show_ext_ref(struct device *dev,
  191. struct device_attribute *attr,
  192. char *buf)
  193. {
  194. struct iio_dev *dev_info = dev_get_drvdata(dev);
  195. struct ad7291_chip_info *chip = dev_info->dev_data;
  196. return sprintf(buf, "%d\n", !!(chip->command & AD7291_EXT_REF));
  197. }
  198. static ssize_t ad7291_store_ext_ref(struct device *dev,
  199. struct device_attribute *attr,
  200. const char *buf,
  201. size_t len)
  202. {
  203. struct iio_dev *dev_info = dev_get_drvdata(dev);
  204. struct ad7291_chip_info *chip = dev_info->dev_data;
  205. u16 command;
  206. int ret;
  207. command = chip->command & (~AD7291_EXT_REF);
  208. if (strcmp(buf, "1"))
  209. command |= AD7291_EXT_REF;
  210. ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
  211. if (ret)
  212. return -EIO;
  213. chip->command = command;
  214. return ret;
  215. }
  216. static IIO_DEVICE_ATTR(ext_ref, S_IRUGO | S_IWUSR,
  217. ad7291_show_ext_ref,
  218. ad7291_store_ext_ref,
  219. 0);
  220. static ssize_t ad7291_show_noise_delay(struct device *dev,
  221. struct device_attribute *attr,
  222. char *buf)
  223. {
  224. struct iio_dev *dev_info = dev_get_drvdata(dev);
  225. struct ad7291_chip_info *chip = dev_info->dev_data;
  226. return sprintf(buf, "%d\n", !!(chip->command & AD7291_NOISE_DELAY));
  227. }
  228. static ssize_t ad7291_store_noise_delay(struct device *dev,
  229. struct device_attribute *attr,
  230. const char *buf,
  231. size_t len)
  232. {
  233. struct iio_dev *dev_info = dev_get_drvdata(dev);
  234. struct ad7291_chip_info *chip = dev_info->dev_data;
  235. u16 command;
  236. int ret;
  237. command = chip->command & (~AD7291_NOISE_DELAY);
  238. if (strcmp(buf, "1"))
  239. command |= AD7291_NOISE_DELAY;
  240. ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
  241. if (ret)
  242. return -EIO;
  243. chip->command = command;
  244. return ret;
  245. }
  246. static IIO_DEVICE_ATTR(noise_delay, S_IRUGO | S_IWUSR,
  247. ad7291_show_noise_delay,
  248. ad7291_store_noise_delay,
  249. 0);
  250. static ssize_t ad7291_show_t_sense(struct device *dev,
  251. struct device_attribute *attr,
  252. char *buf)
  253. {
  254. struct iio_dev *dev_info = dev_get_drvdata(dev);
  255. struct ad7291_chip_info *chip = dev_info->dev_data;
  256. u16 data;
  257. char sign = ' ';
  258. int ret;
  259. ret = ad7291_i2c_read_data(chip, AD7291_T_SENSE, &data);
  260. if (ret)
  261. return -EIO;
  262. if (data & AD7291_T_VALUE_SIGN) {
  263. /* convert supplement to positive value */
  264. data = (AD7291_T_VALUE_SIGN << 1) - data;
  265. sign = '-';
  266. }
  267. return sprintf(buf, "%c%d.%.2d\n", sign,
  268. (data >> AD7291_T_VALUE_FLOAT_OFFSET),
  269. (data & AD7291_T_VALUE_FLOAT_MASK) * 25);
  270. }
  271. static IIO_DEVICE_ATTR(t_sense, S_IRUGO, ad7291_show_t_sense, NULL, 0);
  272. static ssize_t ad7291_show_t_average(struct device *dev,
  273. struct device_attribute *attr,
  274. char *buf)
  275. {
  276. struct iio_dev *dev_info = dev_get_drvdata(dev);
  277. struct ad7291_chip_info *chip = dev_info->dev_data;
  278. u16 data;
  279. char sign = ' ';
  280. int ret;
  281. ret = ad7291_i2c_read_data(chip, AD7291_T_AVERAGE, &data);
  282. if (ret)
  283. return -EIO;
  284. if (data & AD7291_T_VALUE_SIGN) {
  285. /* convert supplement to positive value */
  286. data = (AD7291_T_VALUE_SIGN << 1) - data;
  287. sign = '-';
  288. }
  289. return sprintf(buf, "%c%d.%.2d\n", sign,
  290. (data >> AD7291_T_VALUE_FLOAT_OFFSET),
  291. (data & AD7291_T_VALUE_FLOAT_MASK) * 25);
  292. }
  293. static IIO_DEVICE_ATTR(t_average, S_IRUGO, ad7291_show_t_average, NULL, 0);
  294. static ssize_t ad7291_show_voltage(struct device *dev,
  295. struct device_attribute *attr,
  296. char *buf)
  297. {
  298. struct iio_dev *dev_info = dev_get_drvdata(dev);
  299. struct ad7291_chip_info *chip = dev_info->dev_data;
  300. u16 data[AD7291_VOLTAGE_LIMIT_COUNT];
  301. int i, size, ret;
  302. ret = ad7291_i2c_read_data(chip, AD7291_VOLTAGE, data);
  303. if (ret)
  304. return -EIO;
  305. for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT; i++) {
  306. if (chip->command & (AD7291_T_SENSE_MASK << i)) {
  307. ret = sprintf(buf, "channel[%d]=%d\n", i,
  308. data[i] & AD7291_VALUE_MASK);
  309. if (ret < 0)
  310. break;
  311. buf += ret;
  312. size += ret;
  313. }
  314. }
  315. return size;
  316. }
  317. static IIO_DEVICE_ATTR(voltage, S_IRUGO, ad7291_show_voltage, NULL, 0);
  318. static ssize_t ad7291_show_channel_mask(struct device *dev,
  319. struct device_attribute *attr,
  320. char *buf)
  321. {
  322. struct iio_dev *dev_info = dev_get_drvdata(dev);
  323. struct ad7291_chip_info *chip = dev_info->dev_data;
  324. return sprintf(buf, "0x%x\n", (chip->command & AD7291_VOLTAGE_MASK) >>
  325. AD7291_VOLTAGE_OFFSET);
  326. }
  327. static ssize_t ad7291_store_channel_mask(struct device *dev,
  328. struct device_attribute *attr,
  329. const char *buf,
  330. size_t len)
  331. {
  332. struct iio_dev *dev_info = dev_get_drvdata(dev);
  333. struct ad7291_chip_info *chip = dev_info->dev_data;
  334. u16 command;
  335. unsigned long data;
  336. int i, ret;
  337. ret = strict_strtoul(buf, 16, &data);
  338. if (ret || data > 0xff)
  339. return -EINVAL;
  340. command = chip->command & (~AD7291_VOLTAGE_MASK);
  341. command |= data << AD7291_VOLTAGE_OFFSET;
  342. ret = ad7291_i2c_write(chip, AD7291_COMMAND, command);
  343. if (ret)
  344. return -EIO;
  345. chip->command = command;
  346. for (i = 0, chip->channels = 0; i < AD7291_VOLTAGE_LIMIT_COUNT; i++) {
  347. if (chip->command & (AD7291_T_SENSE_MASK << i))
  348. chip->channels++;
  349. }
  350. return ret;
  351. }
  352. static IIO_DEVICE_ATTR(channel_mask, S_IRUGO | S_IWUSR,
  353. ad7291_show_channel_mask,
  354. ad7291_store_channel_mask,
  355. 0);
  356. static struct attribute *ad7291_attributes[] = {
  357. &iio_dev_attr_available_modes.dev_attr.attr,
  358. &iio_dev_attr_mode.dev_attr.attr,
  359. &iio_dev_attr_reset.dev_attr.attr,
  360. &iio_dev_attr_ext_ref.dev_attr.attr,
  361. &iio_dev_attr_noise_delay.dev_attr.attr,
  362. &iio_dev_attr_t_sense.dev_attr.attr,
  363. &iio_dev_attr_t_average.dev_attr.attr,
  364. &iio_dev_attr_voltage.dev_attr.attr,
  365. &iio_dev_attr_channel_mask.dev_attr.attr,
  366. NULL,
  367. };
  368. static const struct attribute_group ad7291_attribute_group = {
  369. .attrs = ad7291_attributes,
  370. };
  371. /*
  372. * temperature bound events
  373. */
  374. static irqreturn_t ad7291_event_handler(int irq, void *private)
  375. {
  376. struct iio_dev *indio_dev = private;
  377. struct ad7291_chip_info *chip = iio_dev_get_devdata(private);
  378. u16 t_status, v_status;
  379. u16 command;
  380. int i;
  381. s64 timestamp = iio_get_time_ns();
  382. if (ad7291_i2c_read(chip, AD7291_T_ALERT_STATUS, &t_status))
  383. return IRQ_HANDLED;
  384. if (ad7291_i2c_read(chip, AD7291_VOLTAGE_ALERT_STATUS, &v_status))
  385. return IRQ_HANDLED;
  386. if (!(t_status || v_status))
  387. return IRQ_HANDLED;
  388. command = chip->command | AD7291_ALART_CLEAR;
  389. ad7291_i2c_write(chip, AD7291_COMMAND, command);
  390. command = chip->command & ~AD7291_ALART_CLEAR;
  391. ad7291_i2c_write(chip, AD7291_COMMAND, command);
  392. if (t_status & (1 << 0))
  393. iio_push_event(indio_dev, 0,
  394. IIO_UNMOD_EVENT_CODE(IIO_TEMP,
  395. 0,
  396. IIO_EV_TYPE_THRESH,
  397. IIO_EV_DIR_FALLING),
  398. timestamp);
  399. if (t_status & (1 << 1))
  400. iio_push_event(indio_dev, 0,
  401. IIO_UNMOD_EVENT_CODE(IIO_TEMP,
  402. 0,
  403. IIO_EV_TYPE_THRESH,
  404. IIO_EV_DIR_RISING),
  405. timestamp);
  406. if (t_status & (1 << 2))
  407. iio_push_event(indio_dev, 0,
  408. IIO_UNMOD_EVENT_CODE(IIO_TEMP,
  409. 0,
  410. IIO_EV_TYPE_THRESH,
  411. IIO_EV_DIR_FALLING),
  412. timestamp);
  413. if (t_status & (1 << 3))
  414. iio_push_event(indio_dev, 0,
  415. IIO_UNMOD_EVENT_CODE(IIO_TEMP,
  416. 0,
  417. IIO_EV_TYPE_THRESH,
  418. IIO_EV_DIR_RISING),
  419. timestamp);
  420. for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT*2; i += 2) {
  421. if (v_status & (1 << i))
  422. iio_push_event(indio_dev, 0,
  423. IIO_UNMOD_EVENT_CODE(IIO_IN,
  424. i/2,
  425. IIO_EV_TYPE_THRESH,
  426. IIO_EV_DIR_FALLING),
  427. timestamp);
  428. if (v_status & (1 << (i + 1)))
  429. iio_push_event(indio_dev, 0,
  430. IIO_UNMOD_EVENT_CODE(IIO_IN,
  431. i/2,
  432. IIO_EV_TYPE_THRESH,
  433. IIO_EV_DIR_RISING),
  434. timestamp);
  435. }
  436. return IRQ_HANDLED;
  437. }
  438. static inline ssize_t ad7291_show_t_bound(struct device *dev,
  439. struct device_attribute *attr,
  440. char *buf)
  441. {
  442. struct iio_dev *dev_info = dev_get_drvdata(dev);
  443. struct ad7291_chip_info *chip = dev_info->dev_data;
  444. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  445. u16 data;
  446. char sign = ' ';
  447. int ret;
  448. ret = ad7291_i2c_read(chip, this_attr->address, &data);
  449. if (ret)
  450. return -EIO;
  451. data &= AD7291_VALUE_MASK;
  452. if (data & AD7291_T_VALUE_SIGN) {
  453. /* convert supplement to positive value */
  454. data = (AD7291_T_VALUE_SIGN << 1) - data;
  455. sign = '-';
  456. }
  457. return sprintf(buf, "%c%d.%.2d\n", sign,
  458. data >> AD7291_T_VALUE_FLOAT_OFFSET,
  459. (data & AD7291_T_VALUE_FLOAT_MASK) * 25);
  460. }
  461. static inline ssize_t ad7291_set_t_bound(struct device *dev,
  462. struct device_attribute *attr,
  463. const char *buf,
  464. size_t len)
  465. {
  466. struct iio_dev *dev_info = dev_get_drvdata(dev);
  467. struct ad7291_chip_info *chip = dev_info->dev_data;
  468. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  469. long tmp1, tmp2;
  470. u16 data;
  471. char *pos;
  472. int ret;
  473. pos = strchr(buf, '.');
  474. ret = strict_strtol(buf, 10, &tmp1);
  475. if (ret || tmp1 > 127 || tmp1 < -128)
  476. return -EINVAL;
  477. if (pos) {
  478. len = strlen(pos);
  479. if (len > AD7291_T_VALUE_FLOAT_OFFSET)
  480. len = AD7291_T_VALUE_FLOAT_OFFSET;
  481. pos[len] = 0;
  482. ret = strict_strtol(pos, 10, &tmp2);
  483. if (!ret)
  484. tmp2 = (tmp2 / 25) * 25;
  485. }
  486. if (tmp1 < 0)
  487. data = (u16)(-tmp1);
  488. else
  489. data = (u16)tmp1;
  490. data = (data << AD7291_T_VALUE_FLOAT_OFFSET) |
  491. (tmp2 & AD7291_T_VALUE_FLOAT_MASK);
  492. if (tmp1 < 0)
  493. /* convert positive value to supplyment */
  494. data = (AD7291_T_VALUE_SIGN << 1) - data;
  495. ret = ad7291_i2c_write(chip, this_attr->address, data);
  496. if (ret)
  497. return -EIO;
  498. return ret;
  499. }
  500. static inline ssize_t ad7291_show_v_bound(struct device *dev,
  501. struct device_attribute *attr,
  502. u8 bound_reg,
  503. char *buf)
  504. {
  505. struct iio_dev *dev_info = dev_get_drvdata(dev);
  506. struct ad7291_chip_info *chip = dev_info->dev_data;
  507. u16 data;
  508. int ret;
  509. if (bound_reg < AD7291_VOLTAGE_LIMIT_BASE ||
  510. bound_reg >= AD7291_VOLTAGE_LIMIT_BASE +
  511. AD7291_VOLTAGE_LIMIT_COUNT)
  512. return -EINVAL;
  513. ret = ad7291_i2c_read(chip, bound_reg, &data);
  514. if (ret)
  515. return -EIO;
  516. data &= AD7291_VALUE_MASK;
  517. return sprintf(buf, "%d\n", data);
  518. }
  519. static inline ssize_t ad7291_set_v_bound(struct device *dev,
  520. struct device_attribute *attr,
  521. u8 bound_reg,
  522. const char *buf,
  523. size_t len)
  524. {
  525. struct iio_dev *dev_info = dev_get_drvdata(dev);
  526. struct ad7291_chip_info *chip = dev_info->dev_data;
  527. unsigned long value;
  528. u16 data;
  529. int ret;
  530. if (bound_reg < AD7291_VOLTAGE_LIMIT_BASE ||
  531. bound_reg >= AD7291_VOLTAGE_LIMIT_BASE +
  532. AD7291_VOLTAGE_LIMIT_COUNT)
  533. return -EINVAL;
  534. ret = strict_strtoul(buf, 10, &value);
  535. if (ret || value >= 4096)
  536. return -EINVAL;
  537. data = (u16)value;
  538. ret = ad7291_i2c_write(chip, bound_reg, data);
  539. if (ret)
  540. return -EIO;
  541. return ret;
  542. }
  543. static IIO_DEVICE_ATTR(t_sense_high_value,
  544. S_IRUGO | S_IWUSR,
  545. ad7291_show_t_bound, ad7291_set_t_bound,
  546. AD7291_T_SENSE_HIGH);
  547. static IIO_DEVICE_ATTR(t_sense_low_value,
  548. S_IRUGO | S_IWUSR,
  549. ad7291_show_t_bound, ad7291_set_t_bound,
  550. AD7291_T_SENSE_LOW);
  551. static IIO_DEVICE_ATTR(t_sense_hyst_value,
  552. S_IRUGO | S_IWUSR,
  553. ad7291_show_t_bound, ad7291_set_t_bound,
  554. AD7291_T_SENSE_HYST);
  555. static IIO_DEVICE_ATTR(v0_high,
  556. S_IRUGO | S_IWUSR,
  557. ad7291_show_t_bound, ad7291_set_t_bound, 0x04);
  558. static IIO_DEVICE_ATTR(v0_low,
  559. S_IRUGO | S_IWUSR,
  560. ad7291_show_t_bound, ad7291_set_t_bound, 0x05);
  561. static IIO_DEVICE_ATTR(v0_hyst,
  562. S_IRUGO | S_IWUSR,
  563. ad7291_show_t_bound, ad7291_set_t_bound, 0x06);
  564. static IIO_DEVICE_ATTR(v1_high,
  565. S_IRUGO | S_IWUSR,
  566. ad7291_show_t_bound, ad7291_set_t_bound, 0x07);
  567. static IIO_DEVICE_ATTR(v1_low,
  568. S_IRUGO | S_IWUSR,
  569. ad7291_show_t_bound, ad7291_set_t_bound, 0x08);
  570. static IIO_DEVICE_ATTR(v1_hyst,
  571. S_IRUGO | S_IWUSR,
  572. ad7291_show_t_bound, ad7291_set_t_bound, 0x09);
  573. static IIO_DEVICE_ATTR(v2_high,
  574. S_IRUGO | S_IWUSR,
  575. ad7291_show_t_bound, ad7291_set_t_bound, 0x0A);
  576. static IIO_DEVICE_ATTR(v2_low,
  577. S_IRUGO | S_IWUSR,
  578. ad7291_show_t_bound, ad7291_set_t_bound, 0x0B);
  579. static IIO_DEVICE_ATTR(v2_hyst,
  580. S_IRUGO | S_IWUSR,
  581. ad7291_show_t_bound, ad7291_set_t_bound, 0x0C);
  582. static IIO_DEVICE_ATTR(v3_high,
  583. S_IRUGO | S_IWUSR,
  584. /* Datasheet suggests this one and this one only
  585. has the registers in different order */
  586. ad7291_show_t_bound, ad7291_set_t_bound, 0x0E);
  587. static IIO_DEVICE_ATTR(v3_low,
  588. S_IRUGO | S_IWUSR,
  589. ad7291_show_t_bound, ad7291_set_t_bound, 0x0D);
  590. static IIO_DEVICE_ATTR(v3_hyst,
  591. S_IRUGO | S_IWUSR,
  592. ad7291_show_t_bound, ad7291_set_t_bound, 0x0F);
  593. static IIO_DEVICE_ATTR(v4_high,
  594. S_IRUGO | S_IWUSR,
  595. ad7291_show_t_bound, ad7291_set_t_bound, 0x10);
  596. static IIO_DEVICE_ATTR(v4_low,
  597. S_IRUGO | S_IWUSR,
  598. ad7291_show_t_bound, ad7291_set_t_bound, 0x11);
  599. static IIO_DEVICE_ATTR(v4_hyst,
  600. S_IRUGO | S_IWUSR,
  601. ad7291_show_t_bound, ad7291_set_t_bound, 0x12);
  602. static IIO_DEVICE_ATTR(v5_high,
  603. S_IRUGO | S_IWUSR,
  604. ad7291_show_t_bound, ad7291_set_t_bound, 0x13);
  605. static IIO_DEVICE_ATTR(v5_low,
  606. S_IRUGO | S_IWUSR,
  607. ad7291_show_t_bound, ad7291_set_t_bound, 0x14);
  608. static IIO_DEVICE_ATTR(v5_hyst,
  609. S_IRUGO | S_IWUSR,
  610. ad7291_show_t_bound, ad7291_set_t_bound, 0x15);
  611. static IIO_DEVICE_ATTR(v6_high,
  612. S_IRUGO | S_IWUSR,
  613. ad7291_show_t_bound, ad7291_set_t_bound, 0x16);
  614. static IIO_DEVICE_ATTR(v6_low,
  615. S_IRUGO | S_IWUSR,
  616. ad7291_show_t_bound, ad7291_set_t_bound, 0x17);
  617. static IIO_DEVICE_ATTR(v6_hyst,
  618. S_IRUGO | S_IWUSR,
  619. ad7291_show_t_bound, ad7291_set_t_bound, 0x18);
  620. static IIO_DEVICE_ATTR(v7_high,
  621. S_IRUGO | S_IWUSR,
  622. ad7291_show_t_bound, ad7291_set_t_bound, 0x19);
  623. static IIO_DEVICE_ATTR(v7_low,
  624. S_IRUGO | S_IWUSR,
  625. ad7291_show_t_bound, ad7291_set_t_bound, 0x1A);
  626. static IIO_DEVICE_ATTR(v7_hyst,
  627. S_IRUGO | S_IWUSR,
  628. ad7291_show_t_bound, ad7291_set_t_bound, 0x1B);
  629. static struct attribute *ad7291_event_attributes[] = {
  630. &iio_dev_attr_t_sense_high_value.dev_attr.attr,
  631. &iio_dev_attr_t_sense_low_value.dev_attr.attr,
  632. &iio_dev_attr_t_sense_hyst_value.dev_attr.attr,
  633. &iio_dev_attr_v0_high.dev_attr.attr,
  634. &iio_dev_attr_v0_low.dev_attr.attr,
  635. &iio_dev_attr_v0_hyst.dev_attr.attr,
  636. &iio_dev_attr_v1_high.dev_attr.attr,
  637. &iio_dev_attr_v1_low.dev_attr.attr,
  638. &iio_dev_attr_v1_hyst.dev_attr.attr,
  639. &iio_dev_attr_v2_high.dev_attr.attr,
  640. &iio_dev_attr_v2_low.dev_attr.attr,
  641. &iio_dev_attr_v2_hyst.dev_attr.attr,
  642. &iio_dev_attr_v3_high.dev_attr.attr,
  643. &iio_dev_attr_v3_low.dev_attr.attr,
  644. &iio_dev_attr_v3_hyst.dev_attr.attr,
  645. &iio_dev_attr_v4_high.dev_attr.attr,
  646. &iio_dev_attr_v4_low.dev_attr.attr,
  647. &iio_dev_attr_v4_hyst.dev_attr.attr,
  648. &iio_dev_attr_v5_high.dev_attr.attr,
  649. &iio_dev_attr_v5_low.dev_attr.attr,
  650. &iio_dev_attr_v5_hyst.dev_attr.attr,
  651. &iio_dev_attr_v6_high.dev_attr.attr,
  652. &iio_dev_attr_v6_low.dev_attr.attr,
  653. &iio_dev_attr_v6_hyst.dev_attr.attr,
  654. &iio_dev_attr_v7_high.dev_attr.attr,
  655. &iio_dev_attr_v7_low.dev_attr.attr,
  656. &iio_dev_attr_v7_hyst.dev_attr.attr,
  657. NULL,
  658. };
  659. static struct attribute_group ad7291_event_attribute_group = {
  660. .attrs = ad7291_event_attributes,
  661. };
  662. static const struct iio_info ad7291_info = {
  663. .attrs = &ad7291_attribute_group,
  664. .num_interrupt_lines = 1,
  665. .event_attrs = &ad7291_event_attribute_group,
  666. };
  667. /*
  668. * device probe and remove
  669. */
  670. static int __devinit ad7291_probe(struct i2c_client *client,
  671. const struct i2c_device_id *id)
  672. {
  673. struct ad7291_chip_info *chip;
  674. int ret = 0;
  675. chip = kzalloc(sizeof(struct ad7291_chip_info), GFP_KERNEL);
  676. if (chip == NULL)
  677. return -ENOMEM;
  678. /* this is only used for device removal purposes */
  679. i2c_set_clientdata(client, chip);
  680. chip->client = client;
  681. chip->command = AD7291_NOISE_DELAY | AD7291_T_SENSE_MASK;
  682. chip->indio_dev = iio_allocate_device(0);
  683. if (chip->indio_dev == NULL) {
  684. ret = -ENOMEM;
  685. goto error_free_chip;
  686. }
  687. chip->indio_dev->name = id->name;
  688. chip->indio_dev->dev.parent = &client->dev;
  689. chip->indio_dev->info = &ad7291_info;
  690. chip->indio_dev->dev_data = (void *)chip;
  691. chip->indio_dev->modes = INDIO_DIRECT_MODE;
  692. ret = iio_device_register(chip->indio_dev);
  693. if (ret)
  694. goto error_free_dev;
  695. if (client->irq > 0) {
  696. ret = request_threaded_irq(client->irq,
  697. NULL,
  698. &ad7291_event_handler,
  699. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  700. id->name,
  701. chip->indio_dev);
  702. if (ret)
  703. goto error_unreg_dev;
  704. /* set irq polarity low level */
  705. chip->command |= AD7291_ALART_POLARITY;
  706. }
  707. ret = ad7291_i2c_write(chip, AD7291_COMMAND, chip->command);
  708. if (ret) {
  709. ret = -EIO;
  710. goto error_unreg_irq;
  711. }
  712. dev_info(&client->dev, "%s temperature sensor registered.\n",
  713. id->name);
  714. return 0;
  715. error_unreg_irq:
  716. free_irq(client->irq, chip->indio_dev);
  717. error_unreg_dev:
  718. iio_device_unregister(chip->indio_dev);
  719. error_free_dev:
  720. iio_free_device(chip->indio_dev);
  721. error_free_chip:
  722. kfree(chip);
  723. return ret;
  724. }
  725. static int __devexit ad7291_remove(struct i2c_client *client)
  726. {
  727. struct ad7291_chip_info *chip = i2c_get_clientdata(client);
  728. struct iio_dev *indio_dev = chip->indio_dev;
  729. if (client->irq)
  730. free_irq(client->irq, chip->indio_dev);
  731. iio_device_unregister(indio_dev);
  732. iio_free_device(chip->indio_dev);
  733. kfree(chip);
  734. return 0;
  735. }
  736. static const struct i2c_device_id ad7291_id[] = {
  737. { "ad7291", 0 },
  738. {}
  739. };
  740. MODULE_DEVICE_TABLE(i2c, ad7291_id);
  741. static struct i2c_driver ad7291_driver = {
  742. .driver = {
  743. .name = "ad7291",
  744. },
  745. .probe = ad7291_probe,
  746. .remove = __devexit_p(ad7291_remove),
  747. .id_table = ad7291_id,
  748. };
  749. static __init int ad7291_init(void)
  750. {
  751. return i2c_add_driver(&ad7291_driver);
  752. }
  753. static __exit void ad7291_exit(void)
  754. {
  755. i2c_del_driver(&ad7291_driver);
  756. }
  757. MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
  758. MODULE_DESCRIPTION("Analog Devices AD7291 digital"
  759. " temperature sensor driver");
  760. MODULE_LICENSE("GPL v2");
  761. module_init(ad7291_init);
  762. module_exit(ad7291_exit);