PageRenderTime 72ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/staging/iio/addac/adt7316.c

https://bitbucket.org/wisechild/galaxy-nexus
C | 2246 lines | 1804 code | 383 blank | 59 comment | 217 complexity | 67f5fb66e236f1d316630ea599995121 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * ADT7316 digital temperature sensor driver supporting ADT7316/7/8 ADT7516/7/9
  3. *
  4. *
  5. * Copyright 2010 Analog Devices Inc.
  6. *
  7. * Licensed under the GPL-2 or later.
  8. */
  9. #include <linux/interrupt.h>
  10. #include <linux/gpio.h>
  11. #include <linux/workqueue.h>
  12. #include <linux/device.h>
  13. #include <linux/kernel.h>
  14. #include <linux/slab.h>
  15. #include <linux/sysfs.h>
  16. #include <linux/list.h>
  17. #include <linux/i2c.h>
  18. #include <linux/rtc.h>
  19. #include "../iio.h"
  20. #include "../sysfs.h"
  21. #include "adt7316.h"
  22. /*
  23. * ADT7316 registers definition
  24. */
  25. #define ADT7316_INT_STAT1 0x0
  26. #define ADT7316_INT_STAT2 0x1
  27. #define ADT7316_LSB_IN_TEMP_VDD 0x3
  28. #define ADT7316_LSB_IN_TEMP_MASK 0x3
  29. #define ADT7316_LSB_VDD_MASK 0xC
  30. #define ADT7316_LSB_VDD_OFFSET 2
  31. #define ADT7316_LSB_EX_TEMP_AIN 0x4
  32. #define ADT7316_LSB_EX_TEMP_MASK 0x3
  33. #define ADT7516_LSB_AIN_SHIFT 2
  34. #define ADT7316_AD_MSB_DATA_BASE 0x6
  35. #define ADT7316_AD_MSB_DATA_REGS 3
  36. #define ADT7516_AD_MSB_DATA_REGS 6
  37. #define ADT7316_MSB_VDD 0x6
  38. #define ADT7316_MSB_IN_TEMP 0x7
  39. #define ADT7316_MSB_EX_TEMP 0x8
  40. #define ADT7516_MSB_AIN1 0x8
  41. #define ADT7516_MSB_AIN2 0x9
  42. #define ADT7516_MSB_AIN3 0xA
  43. #define ADT7516_MSB_AIN4 0xB
  44. #define ADT7316_DA_DATA_BASE 0x10
  45. #define ADT7316_DA_MSB_DATA_REGS 4
  46. #define ADT7316_LSB_DAC_A 0x10
  47. #define ADT7316_MSB_DAC_A 0x11
  48. #define ADT7316_LSB_DAC_B 0x12
  49. #define ADT7316_MSB_DAC_B 0x13
  50. #define ADT7316_LSB_DAC_C 0x14
  51. #define ADT7316_MSB_DAC_C 0x15
  52. #define ADT7316_LSB_DAC_D 0x16
  53. #define ADT7316_MSB_DAC_D 0x17
  54. #define ADT7316_CONFIG1 0x18
  55. #define ADT7316_CONFIG2 0x19
  56. #define ADT7316_CONFIG3 0x1A
  57. #define ADT7316_LDAC_CONFIG 0x1B
  58. #define ADT7316_DAC_CONFIG 0x1C
  59. #define ADT7316_INT_MASK1 0x1D
  60. #define ADT7316_INT_MASK2 0x1E
  61. #define ADT7316_IN_TEMP_OFFSET 0x1F
  62. #define ADT7316_EX_TEMP_OFFSET 0x20
  63. #define ADT7316_IN_ANALOG_TEMP_OFFSET 0x21
  64. #define ADT7316_EX_ANALOG_TEMP_OFFSET 0x22
  65. #define ADT7316_VDD_HIGH 0x23
  66. #define ADT7316_VDD_LOW 0x24
  67. #define ADT7316_IN_TEMP_HIGH 0x25
  68. #define ADT7316_IN_TEMP_LOW 0x26
  69. #define ADT7316_EX_TEMP_HIGH 0x27
  70. #define ADT7316_EX_TEMP_LOW 0x28
  71. #define ADT7516_AIN2_HIGH 0x2B
  72. #define ADT7516_AIN2_LOW 0x2C
  73. #define ADT7516_AIN3_HIGH 0x2D
  74. #define ADT7516_AIN3_LOW 0x2E
  75. #define ADT7516_AIN4_HIGH 0x2F
  76. #define ADT7516_AIN4_LOW 0x30
  77. #define ADT7316_DEVICE_ID 0x4D
  78. #define ADT7316_MANUFACTURE_ID 0x4E
  79. #define ADT7316_DEVICE_REV 0x4F
  80. #define ADT7316_SPI_LOCK_STAT 0x7F
  81. /*
  82. * ADT7316 config1
  83. */
  84. #define ADT7316_EN 0x1
  85. #define ADT7516_SEL_EX_TEMP 0x4
  86. #define ADT7516_SEL_AIN1_2_EX_TEMP_MASK 0x6
  87. #define ADT7516_SEL_AIN3 0x8
  88. #define ADT7316_INT_EN 0x20
  89. #define ADT7316_INT_POLARITY 0x40
  90. #define ADT7316_PD 0x80
  91. /*
  92. * ADT7316 config2
  93. */
  94. #define ADT7316_AD_SINGLE_CH_MASK 0x3
  95. #define ADT7516_AD_SINGLE_CH_MASK 0x7
  96. #define ADT7316_AD_SINGLE_CH_VDD 0
  97. #define ADT7316_AD_SINGLE_CH_IN 1
  98. #define ADT7316_AD_SINGLE_CH_EX 2
  99. #define ADT7516_AD_SINGLE_CH_AIN1 2
  100. #define ADT7516_AD_SINGLE_CH_AIN2 3
  101. #define ADT7516_AD_SINGLE_CH_AIN3 4
  102. #define ADT7516_AD_SINGLE_CH_AIN4 5
  103. #define ADT7316_AD_SINGLE_CH_MODE 0x10
  104. #define ADT7316_DISABLE_AVERAGING 0x20
  105. #define ADT7316_EN_SMBUS_TIMEOUT 0x40
  106. #define ADT7316_RESET 0x80
  107. /*
  108. * ADT7316 config3
  109. */
  110. #define ADT7316_ADCLK_22_5 0x1
  111. #define ADT7316_DA_HIGH_RESOLUTION 0x2
  112. #define ADT7316_DA_EN_VIA_DAC_LDCA 0x4
  113. #define ADT7516_AIN_IN_VREF 0x10
  114. #define ADT7316_EN_IN_TEMP_PROP_DACA 0x20
  115. #define ADT7316_EN_EX_TEMP_PROP_DACB 0x40
  116. /*
  117. * ADT7316 DAC config
  118. */
  119. #define ADT7316_DA_2VREF_CH_MASK 0xF
  120. #define ADT7316_DA_EN_MODE_MASK 0x30
  121. #define ADT7316_DA_EN_MODE_SINGLE 0x00
  122. #define ADT7316_DA_EN_MODE_AB_CD 0x10
  123. #define ADT7316_DA_EN_MODE_ABCD 0x20
  124. #define ADT7316_DA_EN_MODE_LDAC 0x30
  125. #define ADT7316_VREF_BYPASS_DAC_AB 0x40
  126. #define ADT7316_VREF_BYPASS_DAC_CD 0x80
  127. /*
  128. * ADT7316 LDAC config
  129. */
  130. #define ADT7316_LDAC_EN_DA_MASK 0xF
  131. #define ADT7316_DAC_IN_VREF 0x10
  132. #define ADT7516_DAC_AB_IN_VREF 0x10
  133. #define ADT7516_DAC_CD_IN_VREF 0x20
  134. #define ADT7516_DAC_IN_VREF_OFFSET 4
  135. #define ADT7516_DAC_IN_VREF_MASK 0x30
  136. /*
  137. * ADT7316 INT_MASK2
  138. */
  139. #define ADT7316_INT_MASK2_VDD 0x10
  140. /*
  141. * ADT7316 value masks
  142. */
  143. #define ADT7316_VALUE_MASK 0xfff
  144. #define ADT7316_T_VALUE_SIGN 0x400
  145. #define ADT7316_T_VALUE_FLOAT_OFFSET 2
  146. #define ADT7316_T_VALUE_FLOAT_MASK 0x2
  147. /*
  148. * Chip ID
  149. */
  150. #define ID_ADT7316 0x1
  151. #define ID_ADT7317 0x2
  152. #define ID_ADT7318 0x3
  153. #define ID_ADT7516 0x11
  154. #define ID_ADT7517 0x12
  155. #define ID_ADT7519 0x14
  156. #define ID_FAMILY_MASK 0xF0
  157. #define ID_ADT73XX 0x0
  158. #define ID_ADT75XX 0x10
  159. /*
  160. * struct adt7316_chip_info - chip specifc information
  161. */
  162. struct adt7316_chip_info {
  163. struct iio_dev *indio_dev;
  164. struct adt7316_bus bus;
  165. u16 ldac_pin;
  166. u16 int_mask; /* 0x2f */
  167. u8 config1;
  168. u8 config2;
  169. u8 config3;
  170. u8 dac_config; /* DAC config */
  171. u8 ldac_config; /* LDAC config */
  172. u8 dac_bits; /* 8, 10, 12 */
  173. u8 id; /* chip id */
  174. };
  175. /*
  176. * Logic interrupt mask for user application to enable
  177. * interrupts.
  178. */
  179. #define ADT7316_IN_TEMP_HIGH_INT_MASK 0x1
  180. #define ADT7316_IN_TEMP_LOW_INT_MASK 0x2
  181. #define ADT7316_EX_TEMP_HIGH_INT_MASK 0x4
  182. #define ADT7316_EX_TEMP_LOW_INT_MASK 0x8
  183. #define ADT7316_EX_TEMP_FAULT_INT_MASK 0x10
  184. #define ADT7516_AIN1_INT_MASK 0x4
  185. #define ADT7516_AIN2_INT_MASK 0x20
  186. #define ADT7516_AIN3_INT_MASK 0x40
  187. #define ADT7516_AIN4_INT_MASK 0x80
  188. #define ADT7316_VDD_INT_MASK 0x100
  189. #define ADT7316_TEMP_INT_MASK 0x1F
  190. #define ADT7516_AIN_INT_MASK 0xE0
  191. #define ADT7316_TEMP_AIN_INT_MASK \
  192. (ADT7316_TEMP_INT_MASK | ADT7316_TEMP_INT_MASK)
  193. /*
  194. * struct adt7316_chip_info - chip specifc information
  195. */
  196. struct adt7316_limit_regs {
  197. u16 data_high;
  198. u16 data_low;
  199. };
  200. static ssize_t adt7316_show_enabled(struct device *dev,
  201. struct device_attribute *attr,
  202. char *buf)
  203. {
  204. struct iio_dev *dev_info = dev_get_drvdata(dev);
  205. struct adt7316_chip_info *chip = dev_info->dev_data;
  206. return sprintf(buf, "%d\n", !!(chip->config1 & ADT7316_EN));
  207. }
  208. static ssize_t _adt7316_store_enabled(struct adt7316_chip_info *chip,
  209. int enable)
  210. {
  211. u8 config1;
  212. int ret;
  213. if (enable)
  214. config1 = chip->config1 | ADT7316_EN;
  215. else
  216. config1 = chip->config1 & ~ADT7316_EN;
  217. ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG1, config1);
  218. if (ret)
  219. return -EIO;
  220. chip->config1 = config1;
  221. return ret;
  222. }
  223. static ssize_t adt7316_store_enabled(struct device *dev,
  224. struct device_attribute *attr,
  225. const char *buf,
  226. size_t len)
  227. {
  228. struct iio_dev *dev_info = dev_get_drvdata(dev);
  229. struct adt7316_chip_info *chip = dev_info->dev_data;
  230. int enable;
  231. if (!memcmp(buf, "1", 1))
  232. enable = 1;
  233. else
  234. enable = 0;
  235. if (_adt7316_store_enabled(chip, enable) < 0)
  236. return -EIO;
  237. else
  238. return len;
  239. }
  240. static IIO_DEVICE_ATTR(enabled, S_IRUGO | S_IWUSR,
  241. adt7316_show_enabled,
  242. adt7316_store_enabled,
  243. 0);
  244. static ssize_t adt7316_show_select_ex_temp(struct device *dev,
  245. struct device_attribute *attr,
  246. char *buf)
  247. {
  248. struct iio_dev *dev_info = dev_get_drvdata(dev);
  249. struct adt7316_chip_info *chip = dev_info->dev_data;
  250. if ((chip->id & ID_FAMILY_MASK) != ID_ADT75XX)
  251. return -EPERM;
  252. return sprintf(buf, "%d\n", !!(chip->config1 & ADT7516_SEL_EX_TEMP));
  253. }
  254. static ssize_t adt7316_store_select_ex_temp(struct device *dev,
  255. struct device_attribute *attr,
  256. const char *buf,
  257. size_t len)
  258. {
  259. struct iio_dev *dev_info = dev_get_drvdata(dev);
  260. struct adt7316_chip_info *chip = dev_info->dev_data;
  261. u8 config1;
  262. int ret;
  263. if ((chip->id & ID_FAMILY_MASK) != ID_ADT75XX)
  264. return -EPERM;
  265. config1 = chip->config1 & (~ADT7516_SEL_EX_TEMP);
  266. if (!memcmp(buf, "1", 1))
  267. config1 |= ADT7516_SEL_EX_TEMP;
  268. ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG1, config1);
  269. if (ret)
  270. return -EIO;
  271. chip->config1 = config1;
  272. return len;
  273. }
  274. static IIO_DEVICE_ATTR(select_ex_temp, S_IRUGO | S_IWUSR,
  275. adt7316_show_select_ex_temp,
  276. adt7316_store_select_ex_temp,
  277. 0);
  278. static ssize_t adt7316_show_mode(struct device *dev,
  279. struct device_attribute *attr,
  280. char *buf)
  281. {
  282. struct iio_dev *dev_info = dev_get_drvdata(dev);
  283. struct adt7316_chip_info *chip = dev_info->dev_data;
  284. if (chip->config2 & ADT7316_AD_SINGLE_CH_MODE)
  285. return sprintf(buf, "single_channel\n");
  286. else
  287. return sprintf(buf, "round_robin\n");
  288. }
  289. static ssize_t adt7316_store_mode(struct device *dev,
  290. struct device_attribute *attr,
  291. const char *buf,
  292. size_t len)
  293. {
  294. struct iio_dev *dev_info = dev_get_drvdata(dev);
  295. struct adt7316_chip_info *chip = dev_info->dev_data;
  296. u8 config2;
  297. int ret;
  298. config2 = chip->config2 & (~ADT7316_AD_SINGLE_CH_MODE);
  299. if (!memcmp(buf, "single_channel", 14))
  300. config2 |= ADT7316_AD_SINGLE_CH_MODE;
  301. ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG2, config2);
  302. if (ret)
  303. return -EIO;
  304. chip->config2 = config2;
  305. return len;
  306. }
  307. static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
  308. adt7316_show_mode,
  309. adt7316_store_mode,
  310. 0);
  311. static ssize_t adt7316_show_all_modes(struct device *dev,
  312. struct device_attribute *attr,
  313. char *buf)
  314. {
  315. return sprintf(buf, "single_channel\nround_robin\n");
  316. }
  317. static IIO_DEVICE_ATTR(all_modes, S_IRUGO, adt7316_show_all_modes, NULL, 0);
  318. static ssize_t adt7316_show_ad_channel(struct device *dev,
  319. struct device_attribute *attr,
  320. char *buf)
  321. {
  322. struct iio_dev *dev_info = dev_get_drvdata(dev);
  323. struct adt7316_chip_info *chip = dev_info->dev_data;
  324. if (!(chip->config2 & ADT7316_AD_SINGLE_CH_MODE))
  325. return -EPERM;
  326. switch (chip->config2 & ADT7516_AD_SINGLE_CH_MASK) {
  327. case ADT7316_AD_SINGLE_CH_VDD:
  328. return sprintf(buf, "0 - VDD\n");
  329. case ADT7316_AD_SINGLE_CH_IN:
  330. return sprintf(buf, "1 - Internal Temperature\n");
  331. case ADT7316_AD_SINGLE_CH_EX:
  332. if (((chip->id & ID_FAMILY_MASK) == ID_ADT75XX) &&
  333. (chip->config1 & ADT7516_SEL_AIN1_2_EX_TEMP_MASK) == 0)
  334. return sprintf(buf, "2 - AIN1\n");
  335. else
  336. return sprintf(buf, "2 - External Temperature\n");
  337. case ADT7516_AD_SINGLE_CH_AIN2:
  338. if ((chip->config1 & ADT7516_SEL_AIN1_2_EX_TEMP_MASK) == 0)
  339. return sprintf(buf, "3 - AIN2\n");
  340. else
  341. return sprintf(buf, "N/A\n");
  342. case ADT7516_AD_SINGLE_CH_AIN3:
  343. if (chip->config1 & ADT7516_SEL_AIN3)
  344. return sprintf(buf, "4 - AIN3\n");
  345. else
  346. return sprintf(buf, "N/A\n");
  347. case ADT7516_AD_SINGLE_CH_AIN4:
  348. return sprintf(buf, "5 - AIN4\n");
  349. default:
  350. return sprintf(buf, "N/A\n");
  351. }
  352. }
  353. static ssize_t adt7316_store_ad_channel(struct device *dev,
  354. struct device_attribute *attr,
  355. const char *buf,
  356. size_t len)
  357. {
  358. struct iio_dev *dev_info = dev_get_drvdata(dev);
  359. struct adt7316_chip_info *chip = dev_info->dev_data;
  360. u8 config2;
  361. unsigned long data = 0;
  362. int ret;
  363. if (!(chip->config2 & ADT7316_AD_SINGLE_CH_MODE))
  364. return -EPERM;
  365. ret = strict_strtoul(buf, 10, &data);
  366. if (ret)
  367. return -EINVAL;
  368. if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX) {
  369. if (data > 5)
  370. return -EINVAL;
  371. config2 = chip->config2 & (~ADT7516_AD_SINGLE_CH_MASK);
  372. } else {
  373. if (data > 2)
  374. return -EINVAL;
  375. config2 = chip->config2 & (~ADT7316_AD_SINGLE_CH_MASK);
  376. }
  377. config2 |= data;
  378. ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG2, config2);
  379. if (ret)
  380. return -EIO;
  381. chip->config2 = config2;
  382. return len;
  383. }
  384. static IIO_DEVICE_ATTR(ad_channel, S_IRUGO | S_IWUSR,
  385. adt7316_show_ad_channel,
  386. adt7316_store_ad_channel,
  387. 0);
  388. static ssize_t adt7316_show_all_ad_channels(struct device *dev,
  389. struct device_attribute *attr,
  390. char *buf)
  391. {
  392. struct iio_dev *dev_info = dev_get_drvdata(dev);
  393. struct adt7316_chip_info *chip = dev_info->dev_data;
  394. if (!(chip->config2 & ADT7316_AD_SINGLE_CH_MODE))
  395. return -EPERM;
  396. if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
  397. return sprintf(buf, "0 - VDD\n1 - Internal Temperature\n"
  398. "2 - External Temperature or AIN1\n"
  399. "3 - AIN2\n4 - AIN3\n5 - AIN4\n");
  400. else
  401. return sprintf(buf, "0 - VDD\n1 - Internal Temperature\n"
  402. "2 - External Temperature\n");
  403. }
  404. static IIO_DEVICE_ATTR(all_ad_channels, S_IRUGO,
  405. adt7316_show_all_ad_channels, NULL, 0);
  406. static ssize_t adt7316_show_disable_averaging(struct device *dev,
  407. struct device_attribute *attr,
  408. char *buf)
  409. {
  410. struct iio_dev *dev_info = dev_get_drvdata(dev);
  411. struct adt7316_chip_info *chip = dev_info->dev_data;
  412. return sprintf(buf, "%d\n",
  413. !!(chip->config2 & ADT7316_DISABLE_AVERAGING));
  414. }
  415. static ssize_t adt7316_store_disable_averaging(struct device *dev,
  416. struct device_attribute *attr,
  417. const char *buf,
  418. size_t len)
  419. {
  420. struct iio_dev *dev_info = dev_get_drvdata(dev);
  421. struct adt7316_chip_info *chip = dev_info->dev_data;
  422. u8 config2;
  423. int ret;
  424. config2 = chip->config2 & (~ADT7316_DISABLE_AVERAGING);
  425. if (!memcmp(buf, "1", 1))
  426. config2 |= ADT7316_DISABLE_AVERAGING;
  427. ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG2, config2);
  428. if (ret)
  429. return -EIO;
  430. chip->config2 = config2;
  431. return len;
  432. }
  433. static IIO_DEVICE_ATTR(disable_averaging, S_IRUGO | S_IWUSR,
  434. adt7316_show_disable_averaging,
  435. adt7316_store_disable_averaging,
  436. 0);
  437. static ssize_t adt7316_show_enable_smbus_timeout(struct device *dev,
  438. struct device_attribute *attr,
  439. char *buf)
  440. {
  441. struct iio_dev *dev_info = dev_get_drvdata(dev);
  442. struct adt7316_chip_info *chip = dev_info->dev_data;
  443. return sprintf(buf, "%d\n",
  444. !!(chip->config2 & ADT7316_EN_SMBUS_TIMEOUT));
  445. }
  446. static ssize_t adt7316_store_enable_smbus_timeout(struct device *dev,
  447. struct device_attribute *attr,
  448. const char *buf,
  449. size_t len)
  450. {
  451. struct iio_dev *dev_info = dev_get_drvdata(dev);
  452. struct adt7316_chip_info *chip = dev_info->dev_data;
  453. u8 config2;
  454. int ret;
  455. config2 = chip->config2 & (~ADT7316_EN_SMBUS_TIMEOUT);
  456. if (!memcmp(buf, "1", 1))
  457. config2 |= ADT7316_EN_SMBUS_TIMEOUT;
  458. ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG2, config2);
  459. if (ret)
  460. return -EIO;
  461. chip->config2 = config2;
  462. return len;
  463. }
  464. static IIO_DEVICE_ATTR(enable_smbus_timeout, S_IRUGO | S_IWUSR,
  465. adt7316_show_enable_smbus_timeout,
  466. adt7316_store_enable_smbus_timeout,
  467. 0);
  468. static ssize_t adt7316_store_reset(struct device *dev,
  469. struct device_attribute *attr,
  470. const char *buf,
  471. size_t len)
  472. {
  473. struct iio_dev *dev_info = dev_get_drvdata(dev);
  474. struct adt7316_chip_info *chip = dev_info->dev_data;
  475. u8 config2;
  476. int ret;
  477. config2 = chip->config2 | ADT7316_RESET;
  478. ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG2, config2);
  479. if (ret)
  480. return -EIO;
  481. return len;
  482. }
  483. static IIO_DEVICE_ATTR(reset, S_IWUSR,
  484. NULL,
  485. adt7316_store_reset,
  486. 0);
  487. static ssize_t adt7316_show_powerdown(struct device *dev,
  488. struct device_attribute *attr,
  489. char *buf)
  490. {
  491. struct iio_dev *dev_info = dev_get_drvdata(dev);
  492. struct adt7316_chip_info *chip = dev_info->dev_data;
  493. return sprintf(buf, "%d\n", !!(chip->config1 & ADT7316_PD));
  494. }
  495. static ssize_t adt7316_store_powerdown(struct device *dev,
  496. struct device_attribute *attr,
  497. const char *buf,
  498. size_t len)
  499. {
  500. struct iio_dev *dev_info = dev_get_drvdata(dev);
  501. struct adt7316_chip_info *chip = dev_info->dev_data;
  502. u8 config1;
  503. int ret;
  504. config1 = chip->config1 & (~ADT7316_PD);
  505. if (!memcmp(buf, "1", 1))
  506. config1 |= ADT7316_PD;
  507. ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG1, config1);
  508. if (ret)
  509. return -EIO;
  510. chip->config1 = config1;
  511. return len;
  512. }
  513. static IIO_DEVICE_ATTR(powerdown, S_IRUGO | S_IWUSR,
  514. adt7316_show_powerdown,
  515. adt7316_store_powerdown,
  516. 0);
  517. static ssize_t adt7316_show_fast_ad_clock(struct device *dev,
  518. struct device_attribute *attr,
  519. char *buf)
  520. {
  521. struct iio_dev *dev_info = dev_get_drvdata(dev);
  522. struct adt7316_chip_info *chip = dev_info->dev_data;
  523. return sprintf(buf, "%d\n", !!(chip->config3 & ADT7316_ADCLK_22_5));
  524. }
  525. static ssize_t adt7316_store_fast_ad_clock(struct device *dev,
  526. struct device_attribute *attr,
  527. const char *buf,
  528. size_t len)
  529. {
  530. struct iio_dev *dev_info = dev_get_drvdata(dev);
  531. struct adt7316_chip_info *chip = dev_info->dev_data;
  532. u8 config3;
  533. int ret;
  534. config3 = chip->config3 & (~ADT7316_ADCLK_22_5);
  535. if (!memcmp(buf, "1", 1))
  536. config3 |= ADT7316_ADCLK_22_5;
  537. ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG3, config3);
  538. if (ret)
  539. return -EIO;
  540. chip->config3 = config3;
  541. return len;
  542. }
  543. static IIO_DEVICE_ATTR(fast_ad_clock, S_IRUGO | S_IWUSR,
  544. adt7316_show_fast_ad_clock,
  545. adt7316_store_fast_ad_clock,
  546. 0);
  547. static ssize_t adt7316_show_da_high_resolution(struct device *dev,
  548. struct device_attribute *attr,
  549. char *buf)
  550. {
  551. struct iio_dev *dev_info = dev_get_drvdata(dev);
  552. struct adt7316_chip_info *chip = dev_info->dev_data;
  553. if (chip->config3 & ADT7316_DA_HIGH_RESOLUTION) {
  554. if (chip->id == ID_ADT7316 || chip->id == ID_ADT7516)
  555. return sprintf(buf, "1 (12 bits)\n");
  556. else if (chip->id == ID_ADT7317 || chip->id == ID_ADT7517)
  557. return sprintf(buf, "1 (10 bits)\n");
  558. }
  559. return sprintf(buf, "0 (8 bits)\n");
  560. }
  561. static ssize_t adt7316_store_da_high_resolution(struct device *dev,
  562. struct device_attribute *attr,
  563. const char *buf,
  564. size_t len)
  565. {
  566. struct iio_dev *dev_info = dev_get_drvdata(dev);
  567. struct adt7316_chip_info *chip = dev_info->dev_data;
  568. u8 config3;
  569. int ret;
  570. chip->dac_bits = 8;
  571. if (!memcmp(buf, "1", 1)) {
  572. config3 = chip->config3 | ADT7316_DA_HIGH_RESOLUTION;
  573. if (chip->id == ID_ADT7316 || chip->id == ID_ADT7516)
  574. chip->dac_bits = 12;
  575. else if (chip->id == ID_ADT7317 || chip->id == ID_ADT7517)
  576. chip->dac_bits = 10;
  577. } else
  578. config3 = chip->config3 & (~ADT7316_DA_HIGH_RESOLUTION);
  579. ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG3, config3);
  580. if (ret)
  581. return -EIO;
  582. chip->config3 = config3;
  583. return len;
  584. }
  585. static IIO_DEVICE_ATTR(da_high_resolution, S_IRUGO | S_IWUSR,
  586. adt7316_show_da_high_resolution,
  587. adt7316_store_da_high_resolution,
  588. 0);
  589. static ssize_t adt7316_show_AIN_internal_Vref(struct device *dev,
  590. struct device_attribute *attr,
  591. char *buf)
  592. {
  593. struct iio_dev *dev_info = dev_get_drvdata(dev);
  594. struct adt7316_chip_info *chip = dev_info->dev_data;
  595. if ((chip->id & ID_FAMILY_MASK) != ID_ADT75XX)
  596. return -EPERM;
  597. return sprintf(buf, "%d\n",
  598. !!(chip->config3 & ADT7516_AIN_IN_VREF));
  599. }
  600. static ssize_t adt7316_store_AIN_internal_Vref(struct device *dev,
  601. struct device_attribute *attr,
  602. const char *buf,
  603. size_t len)
  604. {
  605. struct iio_dev *dev_info = dev_get_drvdata(dev);
  606. struct adt7316_chip_info *chip = dev_info->dev_data;
  607. u8 config3;
  608. int ret;
  609. if ((chip->id & ID_FAMILY_MASK) != ID_ADT75XX)
  610. return -EPERM;
  611. if (memcmp(buf, "1", 1))
  612. config3 = chip->config3 & (~ADT7516_AIN_IN_VREF);
  613. else
  614. config3 = chip->config3 | ADT7516_AIN_IN_VREF;
  615. ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG3, config3);
  616. if (ret)
  617. return -EIO;
  618. chip->config3 = config3;
  619. return len;
  620. }
  621. static IIO_DEVICE_ATTR(AIN_internal_Vref, S_IRUGO | S_IWUSR,
  622. adt7316_show_AIN_internal_Vref,
  623. adt7316_store_AIN_internal_Vref,
  624. 0);
  625. static ssize_t adt7316_show_enable_prop_DACA(struct device *dev,
  626. struct device_attribute *attr,
  627. char *buf)
  628. {
  629. struct iio_dev *dev_info = dev_get_drvdata(dev);
  630. struct adt7316_chip_info *chip = dev_info->dev_data;
  631. return sprintf(buf, "%d\n",
  632. !!(chip->config3 & ADT7316_EN_IN_TEMP_PROP_DACA));
  633. }
  634. static ssize_t adt7316_store_enable_prop_DACA(struct device *dev,
  635. struct device_attribute *attr,
  636. const char *buf,
  637. size_t len)
  638. {
  639. struct iio_dev *dev_info = dev_get_drvdata(dev);
  640. struct adt7316_chip_info *chip = dev_info->dev_data;
  641. u8 config3;
  642. int ret;
  643. config3 = chip->config3 & (~ADT7316_EN_IN_TEMP_PROP_DACA);
  644. if (!memcmp(buf, "1", 1))
  645. config3 |= ADT7316_EN_IN_TEMP_PROP_DACA;
  646. ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG3, config3);
  647. if (ret)
  648. return -EIO;
  649. chip->config3 = config3;
  650. return len;
  651. }
  652. static IIO_DEVICE_ATTR(enable_proportion_DACA, S_IRUGO | S_IWUSR,
  653. adt7316_show_enable_prop_DACA,
  654. adt7316_store_enable_prop_DACA,
  655. 0);
  656. static ssize_t adt7316_show_enable_prop_DACB(struct device *dev,
  657. struct device_attribute *attr,
  658. char *buf)
  659. {
  660. struct iio_dev *dev_info = dev_get_drvdata(dev);
  661. struct adt7316_chip_info *chip = dev_info->dev_data;
  662. return sprintf(buf, "%d\n",
  663. !!(chip->config3 & ADT7316_EN_EX_TEMP_PROP_DACB));
  664. }
  665. static ssize_t adt7316_store_enable_prop_DACB(struct device *dev,
  666. struct device_attribute *attr,
  667. const char *buf,
  668. size_t len)
  669. {
  670. struct iio_dev *dev_info = dev_get_drvdata(dev);
  671. struct adt7316_chip_info *chip = dev_info->dev_data;
  672. u8 config3;
  673. int ret;
  674. config3 = chip->config3 & (~ADT7316_EN_EX_TEMP_PROP_DACB);
  675. if (!memcmp(buf, "1", 1))
  676. config3 |= ADT7316_EN_EX_TEMP_PROP_DACB;
  677. ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG3, config3);
  678. if (ret)
  679. return -EIO;
  680. chip->config3 = config3;
  681. return len;
  682. }
  683. static IIO_DEVICE_ATTR(enable_proportion_DACB, S_IRUGO | S_IWUSR,
  684. adt7316_show_enable_prop_DACB,
  685. adt7316_store_enable_prop_DACB,
  686. 0);
  687. static ssize_t adt7316_show_DAC_2Vref_ch_mask(struct device *dev,
  688. struct device_attribute *attr,
  689. char *buf)
  690. {
  691. struct iio_dev *dev_info = dev_get_drvdata(dev);
  692. struct adt7316_chip_info *chip = dev_info->dev_data;
  693. return sprintf(buf, "0x%x\n",
  694. chip->dac_config & ADT7316_DA_2VREF_CH_MASK);
  695. }
  696. static ssize_t adt7316_store_DAC_2Vref_ch_mask(struct device *dev,
  697. struct device_attribute *attr,
  698. const char *buf,
  699. size_t len)
  700. {
  701. struct iio_dev *dev_info = dev_get_drvdata(dev);
  702. struct adt7316_chip_info *chip = dev_info->dev_data;
  703. u8 dac_config;
  704. unsigned long data = 0;
  705. int ret;
  706. ret = strict_strtoul(buf, 16, &data);
  707. if (ret || data > ADT7316_DA_2VREF_CH_MASK)
  708. return -EINVAL;
  709. dac_config = chip->dac_config & (~ADT7316_DA_2VREF_CH_MASK);
  710. dac_config |= data;
  711. ret = chip->bus.write(chip->bus.client, ADT7316_DAC_CONFIG, dac_config);
  712. if (ret)
  713. return -EIO;
  714. chip->dac_config = dac_config;
  715. return len;
  716. }
  717. static IIO_DEVICE_ATTR(DAC_2Vref_channels_mask, S_IRUGO | S_IWUSR,
  718. adt7316_show_DAC_2Vref_ch_mask,
  719. adt7316_store_DAC_2Vref_ch_mask,
  720. 0);
  721. static ssize_t adt7316_show_DAC_update_mode(struct device *dev,
  722. struct device_attribute *attr,
  723. char *buf)
  724. {
  725. struct iio_dev *dev_info = dev_get_drvdata(dev);
  726. struct adt7316_chip_info *chip = dev_info->dev_data;
  727. if (!(chip->config3 & ADT7316_DA_EN_VIA_DAC_LDCA))
  728. return sprintf(buf, "manual\n");
  729. else {
  730. switch (chip->dac_config & ADT7316_DA_EN_MODE_MASK) {
  731. case ADT7316_DA_EN_MODE_SINGLE:
  732. return sprintf(buf, "0 - auto at any MSB DAC writing\n");
  733. case ADT7316_DA_EN_MODE_AB_CD:
  734. return sprintf(buf, "1 - auto at MSB DAC AB and CD writing\n");
  735. case ADT7316_DA_EN_MODE_ABCD:
  736. return sprintf(buf, "2 - auto at MSB DAC ABCD writing\n");
  737. default: /* ADT7316_DA_EN_MODE_LDAC */
  738. return sprintf(buf, "3 - manual\n");
  739. }
  740. }
  741. }
  742. static ssize_t adt7316_store_DAC_update_mode(struct device *dev,
  743. struct device_attribute *attr,
  744. const char *buf,
  745. size_t len)
  746. {
  747. struct iio_dev *dev_info = dev_get_drvdata(dev);
  748. struct adt7316_chip_info *chip = dev_info->dev_data;
  749. u8 dac_config;
  750. unsigned long data;
  751. int ret;
  752. if (!(chip->config3 & ADT7316_DA_EN_VIA_DAC_LDCA))
  753. return -EPERM;
  754. ret = strict_strtoul(buf, 10, &data);
  755. if (ret || data > ADT7316_DA_EN_MODE_MASK)
  756. return -EINVAL;
  757. dac_config = chip->dac_config & (~ADT7316_DA_EN_MODE_MASK);
  758. dac_config |= data;
  759. ret = chip->bus.write(chip->bus.client, ADT7316_DAC_CONFIG, dac_config);
  760. if (ret)
  761. return -EIO;
  762. chip->dac_config = dac_config;
  763. return len;
  764. }
  765. static IIO_DEVICE_ATTR(DAC_update_mode, S_IRUGO | S_IWUSR,
  766. adt7316_show_DAC_update_mode,
  767. adt7316_store_DAC_update_mode,
  768. 0);
  769. static ssize_t adt7316_show_all_DAC_update_modes(struct device *dev,
  770. struct device_attribute *attr,
  771. char *buf)
  772. {
  773. struct iio_dev *dev_info = dev_get_drvdata(dev);
  774. struct adt7316_chip_info *chip = dev_info->dev_data;
  775. if (chip->config3 & ADT7316_DA_EN_VIA_DAC_LDCA)
  776. return sprintf(buf, "0 - auto at any MSB DAC writing\n"
  777. "1 - auto at MSB DAC AB and CD writing\n"
  778. "2 - auto at MSB DAC ABCD writing\n"
  779. "3 - manual\n");
  780. else
  781. return sprintf(buf, "manual\n");
  782. }
  783. static IIO_DEVICE_ATTR(all_DAC_update_modes, S_IRUGO,
  784. adt7316_show_all_DAC_update_modes, NULL, 0);
  785. static ssize_t adt7316_store_update_DAC(struct device *dev,
  786. struct device_attribute *attr,
  787. const char *buf,
  788. size_t len)
  789. {
  790. struct iio_dev *dev_info = dev_get_drvdata(dev);
  791. struct adt7316_chip_info *chip = dev_info->dev_data;
  792. u8 ldac_config;
  793. unsigned long data;
  794. int ret;
  795. if (chip->config3 & ADT7316_DA_EN_VIA_DAC_LDCA) {
  796. if ((chip->dac_config & ADT7316_DA_EN_MODE_MASK) !=
  797. ADT7316_DA_EN_MODE_LDAC)
  798. return -EPERM;
  799. ret = strict_strtoul(buf, 16, &data);
  800. if (ret || data > ADT7316_LDAC_EN_DA_MASK)
  801. return -EINVAL;
  802. ldac_config = chip->ldac_config & (~ADT7316_LDAC_EN_DA_MASK);
  803. ldac_config |= data;
  804. ret = chip->bus.write(chip->bus.client, ADT7316_LDAC_CONFIG,
  805. ldac_config);
  806. if (ret)
  807. return -EIO;
  808. } else {
  809. gpio_set_value(chip->ldac_pin, 0);
  810. gpio_set_value(chip->ldac_pin, 1);
  811. }
  812. return len;
  813. }
  814. static IIO_DEVICE_ATTR(update_DAC, S_IRUGO | S_IWUSR,
  815. NULL,
  816. adt7316_store_update_DAC,
  817. 0);
  818. static ssize_t adt7316_show_DA_AB_Vref_bypass(struct device *dev,
  819. struct device_attribute *attr,
  820. char *buf)
  821. {
  822. struct iio_dev *dev_info = dev_get_drvdata(dev);
  823. struct adt7316_chip_info *chip = dev_info->dev_data;
  824. if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
  825. return -EPERM;
  826. return sprintf(buf, "%d\n",
  827. !!(chip->dac_config & ADT7316_VREF_BYPASS_DAC_AB));
  828. }
  829. static ssize_t adt7316_store_DA_AB_Vref_bypass(struct device *dev,
  830. struct device_attribute *attr,
  831. const char *buf,
  832. size_t len)
  833. {
  834. struct iio_dev *dev_info = dev_get_drvdata(dev);
  835. struct adt7316_chip_info *chip = dev_info->dev_data;
  836. u8 dac_config;
  837. int ret;
  838. if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
  839. return -EPERM;
  840. dac_config = chip->dac_config & (~ADT7316_VREF_BYPASS_DAC_AB);
  841. if (!memcmp(buf, "1", 1))
  842. dac_config |= ADT7316_VREF_BYPASS_DAC_AB;
  843. ret = chip->bus.write(chip->bus.client, ADT7316_DAC_CONFIG, dac_config);
  844. if (ret)
  845. return -EIO;
  846. chip->dac_config = dac_config;
  847. return len;
  848. }
  849. static IIO_DEVICE_ATTR(DA_AB_Vref_bypass, S_IRUGO | S_IWUSR,
  850. adt7316_show_DA_AB_Vref_bypass,
  851. adt7316_store_DA_AB_Vref_bypass,
  852. 0);
  853. static ssize_t adt7316_show_DA_CD_Vref_bypass(struct device *dev,
  854. struct device_attribute *attr,
  855. char *buf)
  856. {
  857. struct iio_dev *dev_info = dev_get_drvdata(dev);
  858. struct adt7316_chip_info *chip = dev_info->dev_data;
  859. if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
  860. return -EPERM;
  861. return sprintf(buf, "%d\n",
  862. !!(chip->dac_config & ADT7316_VREF_BYPASS_DAC_CD));
  863. }
  864. static ssize_t adt7316_store_DA_CD_Vref_bypass(struct device *dev,
  865. struct device_attribute *attr,
  866. const char *buf,
  867. size_t len)
  868. {
  869. struct iio_dev *dev_info = dev_get_drvdata(dev);
  870. struct adt7316_chip_info *chip = dev_info->dev_data;
  871. u8 dac_config;
  872. int ret;
  873. if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
  874. return -EPERM;
  875. dac_config = chip->dac_config & (~ADT7316_VREF_BYPASS_DAC_CD);
  876. if (!memcmp(buf, "1", 1))
  877. dac_config |= ADT7316_VREF_BYPASS_DAC_CD;
  878. ret = chip->bus.write(chip->bus.client, ADT7316_DAC_CONFIG, dac_config);
  879. if (ret)
  880. return -EIO;
  881. chip->dac_config = dac_config;
  882. return len;
  883. }
  884. static IIO_DEVICE_ATTR(DA_CD_Vref_bypass, S_IRUGO | S_IWUSR,
  885. adt7316_show_DA_CD_Vref_bypass,
  886. adt7316_store_DA_CD_Vref_bypass,
  887. 0);
  888. static ssize_t adt7316_show_DAC_internal_Vref(struct device *dev,
  889. struct device_attribute *attr,
  890. char *buf)
  891. {
  892. struct iio_dev *dev_info = dev_get_drvdata(dev);
  893. struct adt7316_chip_info *chip = dev_info->dev_data;
  894. if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
  895. return sprintf(buf, "0x%x\n",
  896. (chip->dac_config & ADT7516_DAC_IN_VREF_MASK) >>
  897. ADT7516_DAC_IN_VREF_OFFSET);
  898. else
  899. return sprintf(buf, "%d\n",
  900. !!(chip->dac_config & ADT7316_DAC_IN_VREF));
  901. }
  902. static ssize_t adt7316_store_DAC_internal_Vref(struct device *dev,
  903. struct device_attribute *attr,
  904. const char *buf,
  905. size_t len)
  906. {
  907. struct iio_dev *dev_info = dev_get_drvdata(dev);
  908. struct adt7316_chip_info *chip = dev_info->dev_data;
  909. u8 ldac_config;
  910. unsigned long data;
  911. int ret;
  912. if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX) {
  913. ret = strict_strtoul(buf, 16, &data);
  914. if (ret || data > 3)
  915. return -EINVAL;
  916. ldac_config = chip->ldac_config & (~ADT7516_DAC_IN_VREF_MASK);
  917. if (data & 0x1)
  918. ldac_config |= ADT7516_DAC_AB_IN_VREF;
  919. else if (data & 0x2)
  920. ldac_config |= ADT7516_DAC_CD_IN_VREF;
  921. } else {
  922. ret = strict_strtoul(buf, 16, &data);
  923. if (ret)
  924. return -EINVAL;
  925. ldac_config = chip->ldac_config & (~ADT7316_DAC_IN_VREF);
  926. if (data)
  927. ldac_config = chip->ldac_config | ADT7316_DAC_IN_VREF;
  928. }
  929. ret = chip->bus.write(chip->bus.client, ADT7316_LDAC_CONFIG, ldac_config);
  930. if (ret)
  931. return -EIO;
  932. chip->ldac_config = ldac_config;
  933. return len;
  934. }
  935. static IIO_DEVICE_ATTR(DAC_internal_Vref, S_IRUGO | S_IWUSR,
  936. adt7316_show_DAC_internal_Vref,
  937. adt7316_store_DAC_internal_Vref,
  938. 0);
  939. static ssize_t adt7316_show_ad(struct adt7316_chip_info *chip,
  940. int channel, char *buf)
  941. {
  942. u16 data;
  943. u8 msb, lsb;
  944. char sign = ' ';
  945. int ret;
  946. if ((chip->config2 & ADT7316_AD_SINGLE_CH_MODE) &&
  947. channel != (chip->config2 & ADT7516_AD_SINGLE_CH_MASK))
  948. return -EPERM;
  949. switch (channel) {
  950. case ADT7316_AD_SINGLE_CH_IN:
  951. ret = chip->bus.read(chip->bus.client,
  952. ADT7316_LSB_IN_TEMP_VDD, &lsb);
  953. if (ret)
  954. return -EIO;
  955. ret = chip->bus.read(chip->bus.client,
  956. ADT7316_AD_MSB_DATA_BASE + channel, &msb);
  957. if (ret)
  958. return -EIO;
  959. data = msb << ADT7316_T_VALUE_FLOAT_OFFSET;
  960. data |= lsb & ADT7316_LSB_IN_TEMP_MASK;
  961. break;
  962. case ADT7316_AD_SINGLE_CH_VDD:
  963. ret = chip->bus.read(chip->bus.client,
  964. ADT7316_LSB_IN_TEMP_VDD, &lsb);
  965. if (ret)
  966. return -EIO;
  967. ret = chip->bus.read(chip->bus.client,
  968. ADT7316_AD_MSB_DATA_BASE + channel, &msb);
  969. if (ret)
  970. return -EIO;
  971. data = msb << ADT7316_T_VALUE_FLOAT_OFFSET;
  972. data |= (lsb & ADT7316_LSB_VDD_MASK) >> ADT7316_LSB_VDD_OFFSET;
  973. return sprintf(buf, "%d\n", data);
  974. default: /* ex_temp and ain */
  975. ret = chip->bus.read(chip->bus.client,
  976. ADT7316_LSB_EX_TEMP_AIN, &lsb);
  977. if (ret)
  978. return -EIO;
  979. ret = chip->bus.read(chip->bus.client,
  980. ADT7316_AD_MSB_DATA_BASE + channel, &msb);
  981. if (ret)
  982. return -EIO;
  983. data = msb << ADT7316_T_VALUE_FLOAT_OFFSET;
  984. data |= lsb & (ADT7316_LSB_EX_TEMP_MASK <<
  985. (ADT7516_LSB_AIN_SHIFT * (channel -
  986. (ADT7316_MSB_EX_TEMP - ADT7316_AD_MSB_DATA_BASE))));
  987. if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
  988. return sprintf(buf, "%d\n", data);
  989. else
  990. break;
  991. }
  992. if (data & ADT7316_T_VALUE_SIGN) {
  993. /* convert supplement to positive value */
  994. data = (ADT7316_T_VALUE_SIGN << 1) - data;
  995. sign = '-';
  996. }
  997. return sprintf(buf, "%c%d.%.2d\n", sign,
  998. (data >> ADT7316_T_VALUE_FLOAT_OFFSET),
  999. (data & ADT7316_T_VALUE_FLOAT_MASK) * 25);
  1000. }
  1001. static ssize_t adt7316_show_VDD(struct device *dev,
  1002. struct device_attribute *attr,
  1003. char *buf)
  1004. {
  1005. struct iio_dev *dev_info = dev_get_drvdata(dev);
  1006. struct adt7316_chip_info *chip = dev_info->dev_data;
  1007. return adt7316_show_ad(chip, ADT7316_AD_SINGLE_CH_VDD, buf);
  1008. }
  1009. static IIO_DEVICE_ATTR(VDD, S_IRUGO, adt7316_show_VDD, NULL, 0);
  1010. static ssize_t adt7316_show_in_temp(struct device *dev,
  1011. struct device_attribute *attr,
  1012. char *buf)
  1013. {
  1014. struct iio_dev *dev_info = dev_get_drvdata(dev);
  1015. struct adt7316_chip_info *chip = dev_info->dev_data;
  1016. return adt7316_show_ad(chip, ADT7316_AD_SINGLE_CH_IN, buf);
  1017. }
  1018. static IIO_DEVICE_ATTR(in_temp, S_IRUGO, adt7316_show_in_temp, NULL, 0);
  1019. static ssize_t adt7316_show_ex_temp_AIN1(struct device *dev,
  1020. struct device_attribute *attr,
  1021. char *buf)
  1022. {
  1023. struct iio_dev *dev_info = dev_get_drvdata(dev);
  1024. struct adt7316_chip_info *chip = dev_info->dev_data;
  1025. return adt7316_show_ad(chip, ADT7316_AD_SINGLE_CH_EX, buf);
  1026. }
  1027. static IIO_DEVICE_ATTR(ex_temp_AIN1, S_IRUGO, adt7316_show_ex_temp_AIN1, NULL, 0);
  1028. static IIO_DEVICE_ATTR(ex_temp, S_IRUGO, adt7316_show_ex_temp_AIN1, NULL, 0);
  1029. static ssize_t adt7316_show_AIN2(struct device *dev,
  1030. struct device_attribute *attr,
  1031. char *buf)
  1032. {
  1033. struct iio_dev *dev_info = dev_get_drvdata(dev);
  1034. struct adt7316_chip_info *chip = dev_info->dev_data;
  1035. return adt7316_show_ad(chip, ADT7516_AD_SINGLE_CH_AIN2, buf);
  1036. }
  1037. static IIO_DEVICE_ATTR(AIN2, S_IRUGO, adt7316_show_AIN2, NULL, 0);
  1038. static ssize_t adt7316_show_AIN3(struct device *dev,
  1039. struct device_attribute *attr,
  1040. char *buf)
  1041. {
  1042. struct iio_dev *dev_info = dev_get_drvdata(dev);
  1043. struct adt7316_chip_info *chip = dev_info->dev_data;
  1044. return adt7316_show_ad(chip, ADT7516_AD_SINGLE_CH_AIN3, buf);
  1045. }
  1046. static IIO_DEVICE_ATTR(AIN3, S_IRUGO, adt7316_show_AIN3, NULL, 0);
  1047. static ssize_t adt7316_show_AIN4(struct device *dev,
  1048. struct device_attribute *attr,
  1049. char *buf)
  1050. {
  1051. struct iio_dev *dev_info = dev_get_drvdata(dev);
  1052. struct adt7316_chip_info *chip = dev_info->dev_data;
  1053. return adt7316_show_ad(chip, ADT7516_AD_SINGLE_CH_AIN4, buf);
  1054. }
  1055. static IIO_DEVICE_ATTR(AIN4, S_IRUGO, adt7316_show_AIN4, NULL, 0);
  1056. static ssize_t adt7316_show_temp_offset(struct adt7316_chip_info *chip,
  1057. int offset_addr, char *buf)
  1058. {
  1059. int data;
  1060. u8 val;
  1061. int ret;
  1062. ret = chip->bus.read(chip->bus.client, offset_addr, &val);
  1063. if (ret)
  1064. return -EIO;
  1065. data = (int)val;
  1066. if (val & 0x80)
  1067. data -= 256;
  1068. return sprintf(buf, "%d\n", data);
  1069. }
  1070. static ssize_t adt7316_store_temp_offset(struct adt7316_chip_info *chip,
  1071. int offset_addr, const char *buf, size_t len)
  1072. {
  1073. long data;
  1074. u8 val;
  1075. int ret;
  1076. ret = strict_strtol(buf, 10, &data);
  1077. if (ret || data > 127 || data < -128)
  1078. return -EINVAL;
  1079. if (data < 0)
  1080. data += 256;
  1081. val = (u8)data;
  1082. ret = chip->bus.write(chip->bus.client, offset_addr, val);
  1083. if (ret)
  1084. return -EIO;
  1085. return len;
  1086. }
  1087. static ssize_t adt7316_show_in_temp_offset(struct device *dev,
  1088. struct device_attribute *attr,
  1089. char *buf)
  1090. {
  1091. struct iio_dev *dev_info = dev_get_drvdata(dev);
  1092. struct adt7316_chip_info *chip = dev_info->dev_data;
  1093. return adt7316_show_temp_offset(chip, ADT7316_IN_TEMP_OFFSET, buf);
  1094. }
  1095. static ssize_t adt7316_store_in_temp_offset(struct device *dev,
  1096. struct device_attribute *attr,
  1097. const char *buf,
  1098. size_t len)
  1099. {
  1100. struct iio_dev *dev_info = dev_get_drvdata(dev);
  1101. struct adt7316_chip_info *chip = dev_info->dev_data;
  1102. return adt7316_store_temp_offset(chip, ADT7316_IN_TEMP_OFFSET, buf, len);
  1103. }
  1104. static IIO_DEVICE_ATTR(in_temp_offset, S_IRUGO | S_IWUSR,
  1105. adt7316_show_in_temp_offset,
  1106. adt7316_store_in_temp_offset, 0);
  1107. static ssize_t adt7316_show_ex_temp_offset(struct device *dev,
  1108. struct device_attribute *attr,
  1109. char *buf)
  1110. {
  1111. struct iio_dev *dev_info = dev_get_drvdata(dev);
  1112. struct adt7316_chip_info *chip = dev_info->dev_data;
  1113. return adt7316_show_temp_offset(chip, ADT7316_EX_TEMP_OFFSET, buf);
  1114. }
  1115. static ssize_t adt7316_store_ex_temp_offset(struct device *dev,
  1116. struct device_attribute *attr,
  1117. const char *buf,
  1118. size_t len)
  1119. {
  1120. struct iio_dev *dev_info = dev_get_drvdata(dev);
  1121. struct adt7316_chip_info *chip = dev_info->dev_data;
  1122. return adt7316_store_temp_offset(chip, ADT7316_EX_TEMP_OFFSET, buf, len);
  1123. }
  1124. static IIO_DEVICE_ATTR(ex_temp_offset, S_IRUGO | S_IWUSR,
  1125. adt7316_show_ex_temp_offset,
  1126. adt7316_store_ex_temp_offset, 0);
  1127. static ssize_t adt7316_show_in_analog_temp_offset(struct device *dev,
  1128. struct device_attribute *attr,
  1129. char *buf)
  1130. {
  1131. struct iio_dev *dev_info = dev_get_drvdata(dev);
  1132. struct adt7316_chip_info *chip = dev_info->dev_data;
  1133. return adt7316_show_temp_offset(chip,
  1134. ADT7316_IN_ANALOG_TEMP_OFFSET, buf);
  1135. }
  1136. static ssize_t adt7316_store_in_analog_temp_offset(struct device *dev,
  1137. struct device_attribute *attr,
  1138. const char *buf,
  1139. size_t len)
  1140. {
  1141. struct iio_dev *dev_info = dev_get_drvdata(dev);
  1142. struct adt7316_chip_info *chip = dev_info->dev_data;
  1143. return adt7316_store_temp_offset(chip,
  1144. ADT7316_IN_ANALOG_TEMP_OFFSET, buf, len);
  1145. }
  1146. static IIO_DEVICE_ATTR(in_analog_temp_offset, S_IRUGO | S_IWUSR,
  1147. adt7316_show_in_analog_temp_offset,
  1148. adt7316_store_in_analog_temp_offset, 0);
  1149. static ssize_t adt7316_show_ex_analog_temp_offset(struct device *dev,
  1150. struct device_attribute *attr,
  1151. char *buf)
  1152. {
  1153. struct iio_dev *dev_info = dev_get_drvdata(dev);
  1154. struct adt7316_chip_info *chip = dev_info->dev_data;
  1155. return adt7316_show_temp_offset(chip,
  1156. ADT7316_EX_ANALOG_TEMP_OFFSET, buf);
  1157. }
  1158. static ssize_t adt7316_store_ex_analog_temp_offset(struct device *dev,
  1159. struct device_attribute *attr,
  1160. const char *buf,
  1161. size_t len)
  1162. {
  1163. struct iio_dev *dev_info = dev_get_drvdata(dev);
  1164. struct adt7316_chip_info *chip = dev_info->dev_data;
  1165. return adt7316_store_temp_offset(chip,
  1166. ADT7316_EX_ANALOG_TEMP_OFFSET, buf, len);
  1167. }
  1168. static IIO_DEVICE_ATTR(ex_analog_temp_offset, S_IRUGO | S_IWUSR,
  1169. adt7316_show_ex_analog_temp_offset,
  1170. adt7316_store_ex_analog_temp_offset, 0);
  1171. static ssize_t adt7316_show_DAC(struct adt7316_chip_info *chip,
  1172. int channel, char *buf)
  1173. {
  1174. u16 data;
  1175. u8 msb, lsb, offset;
  1176. int ret;
  1177. if (channel >= ADT7316_DA_MSB_DATA_REGS ||
  1178. (channel == 0 &&
  1179. (chip->config3 & ADT7316_EN_IN_TEMP_PROP_DACA)) ||
  1180. (channel == 1 &&
  1181. (chip->config3 & ADT7316_EN_EX_TEMP_PROP_DACB)))
  1182. return -EPERM;
  1183. offset = chip->dac_bits - 8;
  1184. if (chip->dac_bits > 8) {
  1185. ret = chip->bus.read(chip->bus.client,
  1186. ADT7316_DA_DATA_BASE + channel * 2, &lsb);
  1187. if (ret)
  1188. return -EIO;
  1189. }
  1190. ret = chip->bus.read(chip->bus.client,
  1191. ADT7316_DA_DATA_BASE + 1 + channel * 2, &msb);
  1192. if (ret)
  1193. return -EIO;
  1194. data = (msb << offset) + (lsb & ((1 << offset) - 1));
  1195. return sprintf(buf, "%d\n", data);
  1196. }
  1197. static ssize_t adt7316_store_DAC(struct adt7316_chip_info *chip,
  1198. int channel, const char *buf, size_t len)
  1199. {
  1200. u8 msb, lsb, offset;
  1201. unsigned long data;
  1202. int ret;
  1203. if (channel >= ADT7316_DA_MSB_DATA_REGS ||
  1204. (channel == 0 &&
  1205. (chip->config3 & ADT7316_EN_IN_TEMP_PROP_DACA)) ||
  1206. (channel == 1 &&
  1207. (chip->config3 & ADT7316_EN_EX_TEMP_PROP_DACB)))
  1208. return -EPERM;
  1209. offset = chip->dac_bits - 8;
  1210. ret = strict_strtoul(buf, 10, &data);
  1211. if (ret || data >= (1 << chip->dac_bits))
  1212. return -EINVAL;
  1213. if (chip->dac_bits > 8) {
  1214. lsb = data & (1 << offset);
  1215. ret = chip->bus.write(chip->bus.client,
  1216. ADT7316_DA_DATA_BASE + channel * 2, lsb);
  1217. if (ret)
  1218. return -EIO;
  1219. }
  1220. msb = data >> offset;
  1221. ret = chip->bus.write(chip->bus.client,
  1222. ADT7316_DA_DATA_BASE + 1 + channel * 2, msb);
  1223. if (ret)
  1224. return -EIO;
  1225. return len;
  1226. }
  1227. static ssize_t adt7316_show_DAC_A(struct device *dev,
  1228. struct device_attribute *attr,
  1229. char *buf)
  1230. {
  1231. struct iio_dev *dev_info = dev_get_drvdata(dev);
  1232. struct adt7316_chip_info *chip = dev_info->dev_data;
  1233. return adt7316_show_DAC(chip, 0, buf);
  1234. }
  1235. static ssize_t adt7316_store_DAC_A(struct device *dev,
  1236. struct device_attribute *attr,
  1237. const char *buf,
  1238. size_t len)
  1239. {
  1240. struct iio_dev *dev_info = dev_get_drvdata(dev);
  1241. struct adt7316_chip_info *chip = dev_info->dev_data;
  1242. return adt7316_store_DAC(chip, 0, buf, len);
  1243. }
  1244. static IIO_DEVICE_ATTR(DAC_A, S_IRUGO | S_IWUSR, adt7316_show_DAC_A,
  1245. adt7316_store_DAC_A, 0);
  1246. static ssize_t adt7316_show_DAC_B(struct device *dev,
  1247. struct device_attribute *attr,
  1248. char *buf)
  1249. {
  1250. struct iio_dev *dev_info = dev_get_drvdata(dev);
  1251. struct adt7316_chip_info *chip = dev_info->dev_data;
  1252. return adt7316_show_DAC(chip, 1, buf);
  1253. }
  1254. static ssize_t adt7316_store_DAC_B(struct device *dev,
  1255. struct device_attribute *attr,
  1256. const char *buf,
  1257. size_t len)
  1258. {
  1259. struct iio_dev *dev_info = dev_get_drvdata(dev);
  1260. struct adt7316_chip_info *chip = dev_info->dev_data;
  1261. return adt7316_store_DAC(chip, 1, buf, len);
  1262. }
  1263. static IIO_DEVICE_ATTR(DAC_B, S_IRUGO | S_IWUSR, adt7316_show_DAC_B,
  1264. adt7316_store_DAC_B, 0);
  1265. static ssize_t adt7316_show_DAC_C(struct device *dev,
  1266. struct device_attribute *attr,
  1267. char *buf)
  1268. {
  1269. struct iio_dev *dev_info = dev_get_drvdata(dev);
  1270. struct adt7316_chip_info *chip = dev_info->dev_data;
  1271. return adt7316_show_DAC(chip, 2, buf);
  1272. }
  1273. static ssize_t adt7316_store_DAC_C(struct device *dev,
  1274. struct device_attribute *attr,
  1275. const char *buf,
  1276. size_t len)
  1277. {
  1278. struct iio_dev *dev_info = dev_get_drvdata(dev);
  1279. struct adt7316_chip_info *chip = dev_info->dev_data;
  1280. return adt7316_store_DAC(chip, 2, buf, len);
  1281. }
  1282. static IIO_DEVICE_ATTR(DAC_C, S_IRUGO | S_IWUSR, adt7316_show_DAC_C,
  1283. adt7316_store_DAC_C, 0);
  1284. static ssize_t adt7316_show_DAC_D(struct device *dev,
  1285. struct device_attribute *attr,
  1286. char *buf)
  1287. {
  1288. struct iio_dev *dev_info = dev_get_drvdata(dev);
  1289. struct adt7316_chip_info *chip = dev_info->dev_data;
  1290. return adt7316_show_DAC(chip, 3, buf);
  1291. }
  1292. static ssize_t adt7316_store_DAC_D(struct device *dev,
  1293. struct device_attribute *attr,
  1294. const char *buf,
  1295. size_t len)
  1296. {
  1297. struct iio_dev *dev_info = dev_get_drvdata(dev);
  1298. struct adt7316_chip_info *chip = dev_info->dev_data;
  1299. return adt7316_store_DAC(chip, 3, buf, len);
  1300. }
  1301. static IIO_DEVICE_ATTR(DAC_D, S_IRUGO | S_IWUSR, adt7316_show_DAC_D,
  1302. adt7316_store_DAC_D, 0);
  1303. static ssize_t adt7316_show_device_id(struct device *dev,
  1304. struct device_attribute *attr,
  1305. char *buf)
  1306. {
  1307. struct iio_dev *dev_info = dev_get_drvdata(dev);
  1308. struct adt7316_chip_info *chip = dev_info->dev_data;
  1309. u8 id;
  1310. int ret;
  1311. ret = chip->bus.read(chip->bus.client, ADT7316_DEVICE_ID, &id);
  1312. if (ret)
  1313. return -EIO;
  1314. return sprintf(buf, "%d\n", id);
  1315. }
  1316. static IIO_DEVICE_ATTR(device_id, S_IRUGO, adt7316_show_device_id, NULL, 0);
  1317. static ssize_t adt7316_show_manufactorer_id(struct device *dev,
  1318. struct device_attribute *attr,
  1319. char *buf)
  1320. {
  1321. struct iio_dev *dev_info = dev_get_drvdata(dev);
  1322. struct adt7316_chip_info *chip = dev_info->dev_data;
  1323. u8 id;
  1324. int ret;
  1325. ret = chip->bus.read(chip->bus.client, ADT7316_MANUFACTURE_ID, &id);
  1326. if (ret)
  1327. return -EIO;
  1328. return sprintf(buf, "%d\n", id);
  1329. }
  1330. static IIO_DEVICE_ATTR(manufactorer_id, S_IRUGO,
  1331. adt7316_show_manufactorer_id, NULL, 0);
  1332. static ssize_t adt7316_show_device_rev(struct device *dev,
  1333. struct device_attribute *attr,
  1334. char *buf)
  1335. {
  1336. struct iio_dev *dev_info = dev_get_drvdata(dev);
  1337. struct adt7316_chip_info *chip = dev_info->dev_data;
  1338. u8 rev;
  1339. int ret;
  1340. ret = chip->bus.read(chip->bus.client, ADT7316_DEVICE_REV, &rev);
  1341. if (ret)
  1342. return -EIO;
  1343. return sprintf(buf, "%d\n", rev);
  1344. }
  1345. static IIO_DEVICE_ATTR(device_rev, S_IRUGO, adt7316_show_device_rev, NULL, 0);
  1346. static ssize_t adt7316_show_bus_type(struct device *dev,
  1347. struct device_attribute *attr,
  1348. char *buf)
  1349. {
  1350. struct iio_dev *dev_info = dev_get_drvdata(dev);
  1351. struct adt7316_chip_info *chip = dev_info->dev_data;
  1352. u8 stat;
  1353. int ret;
  1354. ret = chip->bus.read(chip->bus.client, ADT7316_SPI_LOCK_STAT, &stat);
  1355. if (ret)
  1356. return -EIO;
  1357. if (stat)
  1358. return sprintf(buf, "spi\n");
  1359. else
  1360. return sprintf(buf, "i2c\n");
  1361. }
  1362. static IIO_DEVICE_ATTR(bus_type, S_IRUGO, adt7316_show_bus_type, NULL, 0);
  1363. static struct attribute *adt7316_attributes[] = {
  1364. &iio_dev_attr_all_modes.dev_attr.attr,
  1365. &iio_dev_attr_mode.dev_attr.attr,
  1366. &iio_dev_attr_reset.dev_attr.attr,
  1367. &iio_dev_attr_enabled.dev_attr.attr,
  1368. &iio_dev_attr_ad_channel.dev_attr.attr,
  1369. &iio_dev_attr_all_ad_channels.dev_attr.attr,
  1370. &iio_dev_attr_disable_averaging.dev_attr.attr,
  1371. &iio_dev_attr_enable_smbus_timeout.dev_attr.attr,
  1372. &iio_dev_attr_powerdown.dev_attr.attr,
  1373. &iio_dev_attr_fast_ad_clock.dev_attr.attr,
  1374. &iio_dev_attr_da_high_resolution.dev_attr.attr,
  1375. &iio_dev_attr_enable_proportion_DACA.dev_attr.attr,
  1376. &iio_dev_attr_enable_proportion_DACB.dev_attr.attr,
  1377. &iio_dev_attr_DAC_2Vref_channels_mask.dev_attr.attr,
  1378. &iio_dev_attr_DAC_update_mode.dev_attr.attr,
  1379. &iio_dev_attr_all_DAC_update_modes.dev_attr.attr,
  1380. &iio_dev_attr_update_DAC.dev_attr.attr,
  1381. &iio_dev_attr_DA_AB_Vref_bypass.dev_attr.attr,
  1382. &iio_dev_attr_DA_CD_Vref_bypass.dev_attr.attr,
  1383. &iio_dev_attr_DAC_internal_Vref.dev_attr.attr,
  1384. &iio_dev_attr_VDD.dev_attr.attr,
  1385. &iio_dev_attr_in_temp.dev_attr.attr,
  1386. &iio_dev_attr_ex_temp.dev_attr.attr,
  1387. &iio_dev_attr_in_temp_offset.dev_attr.attr,
  1388. &iio_dev_attr_ex_temp_offset.dev_attr.attr,
  1389. &iio_dev_attr_in_analog_temp_offset.dev_attr.attr,
  1390. &iio_dev_attr_ex_analog_temp_offset.dev_attr.attr,
  1391. &iio_dev_attr_DAC_A.dev_attr.attr,
  1392. &iio_dev_attr_DAC_B.dev_attr.attr,
  1393. &iio_dev_attr_DAC_C.dev_attr.attr,
  1394. &iio_dev_attr_DAC_D.dev_attr.attr,
  1395. &iio_dev_attr_device_id.dev_attr.attr,
  1396. &iio_dev_attr_manufactorer_id.dev_attr.attr,
  1397. &iio_dev_attr_device_rev.dev_attr.attr,
  1398. &iio_dev_attr_bus_type.dev_attr.attr,
  1399. NULL,
  1400. };
  1401. static const struct attribute_group adt7316_attribute_group = {
  1402. .attrs = adt7316_attributes,
  1403. };
  1404. static struct attribute *adt7516_attributes[] = {
  1405. &iio_dev_attr_all_modes.dev_attr.attr,
  1406. &iio_dev_attr_mode.dev_attr.attr,
  1407. &iio_dev_attr_select_ex_temp.dev_attr.attr,
  1408. &iio_dev_attr_reset.dev_attr.attr,
  1409. &iio_dev_attr_enabled.dev_attr.attr,
  1410. &iio_dev_attr_ad_channel.dev_attr.attr,
  1411. &iio_dev_attr_all_ad_channels.dev_attr.attr,
  1412. &iio_dev_attr_disable_averaging.dev_attr.attr,
  1413. &iio_dev_attr_enable_smbus_timeout.dev_attr.attr,
  1414. &iio_dev_attr_powerdown.dev_attr.attr,
  1415. &iio_dev_attr_fast_ad_clock.dev_attr.attr,
  1416. &iio_dev_attr_AIN_internal_Vref.dev_attr.attr,
  1417. &iio_dev_attr_da_high_resolution.dev_attr.attr,
  1418. &iio_dev_attr_enable_proportion_DACA.dev_attr.attr,
  1419. &iio_dev_attr_enable_proportion_DACB.dev_attr.attr,
  1420. &iio_dev_attr_DAC_2Vref_channels_mask.dev_attr.attr,
  1421. &iio_dev_attr_DAC_update_mode.dev_attr.attr,
  1422. &iio_dev_attr_all_DAC_update_modes.dev_attr.attr,
  1423. &iio_dev_attr_update_DAC.dev_attr.attr,
  1424. &iio_dev_attr_DA_AB_Vref_bypass.dev_attr.attr,
  1425. &iio_dev_attr_DA_CD_Vref_bypass.dev_attr.attr,
  1426. &iio_dev_attr_DAC_internal_Vref.dev_attr.attr,
  1427. &iio_dev_attr_VDD.dev_attr.attr,
  1428. &iio_dev_attr_in_temp.dev_attr.attr,
  1429. &iio_dev_attr_ex_temp_AIN1.dev_attr.attr,
  1430. &iio_dev_attr_AIN2.dev_attr.attr,
  1431. &iio_dev_attr_AIN3.dev_attr.attr,
  1432. &iio_dev_attr_AIN4.dev_attr.attr,
  1433. &iio_dev_attr_in_temp_offset.dev_attr.attr,
  1434. &iio_dev_attr_ex_temp_offset.dev_attr.attr,
  1435. &iio_dev_attr_in_analog_temp_offset.dev_attr.attr,
  1436. &iio_dev_attr_ex_analog_temp_offset.dev_attr.attr,
  1437. &iio_dev_attr_DAC_A.dev_attr.attr,
  1438. &iio_dev_attr_DAC_B.dev_attr.attr,
  1439. &iio_dev_attr_DAC_C.dev_attr.attr,
  1440. &iio_dev_attr_DAC_D.dev_attr.attr,
  1441. &iio_dev_attr_device_id.dev_attr.attr,
  1442. &iio_dev_attr_manufactorer_id.dev_attr.attr,
  1443. &iio_dev_attr_device_rev.dev_attr.attr,
  1444. &iio_dev_attr_bus_type.dev_attr.attr,
  1445. NULL,
  1446. };
  1447. static const struct attribute_group adt7516_attribute_group = {
  1448. .attrs = adt7516_attributes,
  1449. };
  1450. static irqreturn_t adt7316_event_handler(int irq, void *private)
  1451. {
  1452. struct iio_dev *indio_dev = private;
  1453. struct adt7316_chip_info *chip = iio_dev_get_devdata(indio_dev);
  1454. u8 stat1, stat2;
  1455. int ret;
  1456. s64 time;
  1457. ret = chip->bus.read(chip->bus.client, ADT7316_INT_STAT1, &stat1);
  1458. if (!ret) {
  1459. if ((chip->id & ID_FAMILY_MASK) != ID_ADT75XX)
  1460. stat1 &= 0x1F;
  1461. time = iio_get_time_ns();
  1462. if (stat1 & (1 << 0))
  1463. iio_push_event(chip->indio_dev, 0,
  1464. IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
  1465. IIO_EV_TYPE_THRESH,
  1466. IIO_EV_DIR_RISING),
  1467. time);
  1468. if (stat1 & (1 << 1))
  1469. iio_push_event(chip->indio_dev, 0,
  1470. IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
  1471. IIO_EV_TYPE_THRESH,
  1472. IIO_EV_DIR_FALLING),
  1473. time);
  1474. if (stat1 & (1 << 2))
  1475. iio_push_event(chip->indio_dev, 0,
  1476. IIO_UNMOD_EVENT_CODE(IIO_TEMP, 1,
  1477. IIO_EV_TYPE_THRESH,
  1478. IIO_EV_DIR_RISING),
  1479. time);
  1480. if (stat1 & (1 << 3))
  1481. iio_push_event(chip->indio_dev, 0,
  1482. IIO_UNMOD_EVENT_CODE(IIO_TEMP, 1,
  1483. IIO_EV_TYPE_THRESH,
  1484. IIO_EV_DIR_FALLING),
  1485. time);
  1486. if (stat1 & (1 << 5))
  1487. iio_push_event(chip->indio_dev, 0,
  1488. IIO_UNMOD_EVENT_CODE(IIO_IN, 1,
  1489. IIO_EV_TYPE_THRESH,
  1490. IIO_EV_DIR_EITHER),
  1491. time);
  1492. if (stat1 & (1 << 6))
  1493. iio_push_event(chip->indio_dev, 0,
  1494. IIO_UNMOD_EVENT_CODE(IIO_IN, 2,
  1495. IIO_EV_TYPE_THRESH,
  1496. IIO_EV_DIR_EITHER),
  1497. time);
  1498. if (stat1 & (1 << 7))
  1499. iio_push_event(chip->indio_dev, 0,
  1500. IIO_UNMOD_EVENT_CODE(IIO_IN, 3,
  1501. IIO_EV_TYPE_THRESH,
  1502. IIO_EV_DIR_EITHER),
  1503. time);
  1504. }
  1505. ret = chip->bus.read(chip->bus.client, ADT7316_INT_STAT2, &stat2);
  1506. if (!ret) {
  1507. if (stat2 & ADT7316_INT_MASK2_VDD)
  1508. iio_push_event(chip->indio_dev, 0,
  1509. IIO_UNMOD_EVENT_CODE(IIO_IN,
  1510. 0,
  1511. IIO_EV_TYPE_THRESH,
  1512. IIO_EV_DIR_RISING),
  1513. iio_get_time_ns());
  1514. }
  1515. return IRQ_HANDLED;
  1516. }
  1517. /*
  1518. * Show mask of enabled interrupts in Hex.
  1519. */
  1520. static ssize_t adt7316_show_int_mask(struct device *dev,
  1521. struct device_attribute *attr,
  1522. char *buf)
  1523. {
  1524. struct iio_dev *dev_info = dev_get_drvdata(dev);
  1525. struct adt7316_chip_info *chip = dev_info->dev_data;
  1526. return sprintf(buf, "0x%x\n", chip->int_mask);
  1527. }
  1528. /*
  1529. * Set 1 to the mask in Hex to enabled interrupts.
  1530. */
  1531. static ssize_t adt7316_set_int_mask(struct device *dev,
  1532. struct device_attribute *attr,
  1533. const char *buf,
  1534. size_t len)
  1535. {
  1536. struct iio_dev *dev_info = dev_get_drvdata(dev);
  1537. struct adt7316_chip_info *chip = dev_info->dev_data;
  1538. unsigned long data;
  1539. int ret;
  1540. u8 mask;
  1541. ret = strict_strtoul(buf, 16, &data);
  1542. if (ret || data >= ADT7316_VDD_INT_MASK + 1)
  1543. return -EINVAL;
  1544. if (data & ADT7316_VDD_INT_MASK)
  1545. mask = 0; /* enable vdd int */
  1546. else
  1547. mask = ADT7316_INT_MASK2_VDD; /* disable vdd int */
  1548. ret = chip->bus.write(chip->bus.client, ADT7316_INT_MASK2, mask);
  1549. if (!ret) {
  1550. chip->int_mask &= ~ADT7316_VDD_INT_MASK;
  1551. chip->int_mask |= data & ADT7316_VDD_INT_MASK;
  1552. }
  1553. if (data & ADT7316_TEMP_AIN_INT_MASK) {
  1554. if ((chip->id & ID_FAMILY_MASK) == ID_ADT73XX)
  1555. /* mask in reg is opposite, set 1 to disable */
  1556. mask = (~data) & ADT7316_TEMP_INT_MASK;
  1557. else
  1558. /* mask in reg is opposite, set 1 to disable */
  1559. mask = (~data) & ADT7316_TEMP_AIN_INT_MASK;
  1560. }
  1561. ret = chip->bus.write(chip->bus.client, ADT7316_INT_MASK1, mask);
  1562. chip->int_mask = mask;
  1563. return len;
  1564. }
  1565. static inline ssize_t adt7316_show_ad_bound(struct device *dev,
  1566. struct device_attribute *attr,
  1567. char *buf)
  1568. {
  1569. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  1570. struct iio_dev *dev_info = dev_get_drvdata(dev);
  1571. struct adt7316_chip_info *chip = dev_info->dev_data;
  1572. u8 val;
  1573. int data;
  1574. int ret;
  1575. if ((chip->id & ID_FAMILY_MASK) == ID_ADT73XX &&
  1576. this_attr->address > ADT7316_EX_TEMP_LOW)
  1577. return -EPERM;
  1578. ret = chip->bus.read(chip->bus.client, this_attr->address, &val);
  1579. if (ret)
  1580. return -EIO;
  1581. data = (int)val;
  1582. if (!((chip->id & ID_FAMILY_MASK) == ID_ADT75XX &&
  1583. (chip->config1 & ADT7516_SEL_AIN1_2_EX_TEMP_MASK) == 0)) {
  1584. if (data & 0x80)
  1585. data -= 256;
  1586. }
  1587. return sprintf(buf, "%d\n", data);
  1588. }
  1589. static inline ssize_t adt7316_set_ad_bound(struct device *dev,
  1590. struct device_attribute *attr,
  1591. const char *buf,
  1592. size_t len)
  1593. {
  1594. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  1595. struct iio_dev *dev_info = dev_get_drvdata(dev);
  1596. struct adt7316_chip_info *chip = dev_info->dev_data;
  1597. long data;
  1598. u8 val;
  1599. int ret;
  1600. if ((chip->id & ID_FAMILY_MASK) == ID_ADT73XX &&
  1601. this_attr->address > ADT7316_EX_TEMP_LOW)
  1602. return -EPERM;
  1603. ret = strict_strtol(buf, 10, &data);
  1604. if (ret)
  1605. return -EINVAL;
  1606. if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX &&
  1607. (chip->config1 & ADT7516_SEL_AIN1_2_EX_TEMP_MASK) == 0) {
  1608. if (data > 255 || data < 0)
  1609. return -EINVAL;
  1610. } else {
  1611. if (data > 127 || data < -128)
  1612. return -EINVAL;
  1613. if (data < 0)
  1614. data += 256;
  1615. }
  1616. val = (u8)data;
  1617. ret = chip->bus.write(chip->bus.client, this_attr->address, val);
  1618. if (ret)
  1619. return -EIO;
  1620. return len;
  1621. }
  1622. static ssize_t adt7316_show_int_enabled(struct device *dev,
  1623. struct device_attribute *attr,
  1624. char *buf)
  1625. {
  1626. struct iio_dev *dev_info = dev_get_drvdata(dev);
  1627. struct adt7316_chip_info *chip = dev_info->dev_data;
  1628. return sprintf(buf, "%d\n", !!(chip->config1 & ADT7316_INT_EN));
  1629. }
  1630. static ssize_t adt7316_set_int_enabled(struct device *dev,
  1631. struct device_attribute *attr,
  1632. const char *buf,
  1633. size_t len)
  1634. {
  1635. struct iio_dev *dev_info = dev_get_drvdata(dev);
  1636. struct adt7316_chip_info *chip = dev_info->dev_data;
  1637. u8 config1;
  1638. int ret;
  1639. config1 = chip->config1 & (~ADT7316_INT_EN);
  1640. if (!memcmp(buf, "1", 1))
  1641. config1 |= ADT7316_INT_EN;
  1642. ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG1, config1);
  1643. if (ret)
  1644. return -EIO;
  1645. chip->config1 = config1;
  1646. return len;
  1647. }
  1648. static IIO_DEVICE_ATTR(int_mask,
  1649. S_IRUGO | S_IWUSR,
  1650. adt7316_show_int_mask, adt7316_set_int_mask,
  1651. 0);
  1652. static IIO_DEVICE_ATTR(in_temp_high_value,
  1653. S_IRUGO | S_IWUSR,
  1654. adt7316_show_ad_bound, adt7316_set_ad_bound,
  1655. ADT7316_IN_TEMP_HIGH);
  1656. static IIO_DEVICE_ATTR(in_temp_low_value,
  1657. S_IRUGO | S_IWUSR,
  1658. adt7316_show_ad_bound, adt7316_set_ad_bound,
  1659. ADT7316_IN_TEMP_LOW);
  1660. static IIO_DEVICE_ATTR(ex_temp_high_value,
  1661. S_IRUGO | S_IWUSR,
  1662. adt7316_show_ad_bound, adt7316_set_ad_bound,
  1663. ADT7316_EX_TEMP_HIGH);
  1664. static IIO_DEVICE_ATTR(ex_temp_low_value,
  1665. S_IRUGO | S_IWUSR,
  1666. adt7316_show_ad_bound, adt7316_set_ad_bound,
  1667. ADT7316_EX_TEMP_LOW);
  1668. /* NASTY duplication to be fixed */
  1669. static IIO_DEVICE_ATTR(ex_temp_ain1_high_value,
  1670. S_IRUGO | S_IWUSR,
  1671. adt7316_show_ad_bound, adt7316_set_ad_bound,
  1672. ADT7316_EX_TEMP_HIGH);
  1673. static IIO_DEVICE_ATTR(ex_temp_ain1_low_value,
  1674. S_IRUGO | S_IWUSR,
  1675. adt7316_show_ad_bound, adt7316_set_ad_bound,
  1676. ADT7316_EX_TEMP_LOW);
  1677. static IIO_DEVICE_ATTR(ain2_high_value,
  1678. S_IRUGO | S_IWUSR,
  1679. adt7316_show_ad_bound, adt7316_set_ad_bound,
  1680. ADT7516_AIN2_HIGH);
  1681. static IIO_DEVICE_ATTR(ain2_low_value,
  1682. S_IRUGO | S_IWUSR,
  1683. adt7316_show_ad_bound, adt7316_set_ad_bound,
  1684. ADT7516_AIN2_LOW);
  1685. static IIO_DEVICE_ATTR(ain3_high_value,
  1686. S_IRUGO | S_IWUSR,
  1687. adt7316_show_ad_bound, adt7316_set_ad_bound,
  1688. ADT7516_AIN3_HIGH);
  1689. static IIO_DEVICE_ATTR(ain3_low_value,
  1690. S_IRUGO | S_IWUSR,
  1691. adt7316_show_ad_bound, adt7316_set_ad_bound,
  1692. ADT7516_AIN3_LOW);
  1693. static IIO_DEVICE_ATTR(ain4_high_value,
  1694. S_IRUGO | S_IWUSR,
  1695. adt7316_show_ad_bound, adt7316_set_ad_bound,
  1696. ADT7516_AIN4_HIGH);
  1697. static IIO_DEVICE_ATTR(ain4_low_value,
  1698. S_IRUGO | S_IWUSR,
  1699. adt7316_show_ad_bound, adt7316_set_ad_bound,
  1700. ADT7516_AIN4_LOW);
  1701. static IIO_DEVICE_ATTR(int_enabled,
  1702. S_IRUGO | S_IWUSR,
  1703. adt7316_show_int_enabled,
  1704. adt7316_set_int_enabled, 0);
  1705. static struct attribute *adt7316_event_attributes[] = {
  1706. &iio_dev_attr_int_mask.dev_attr.attr,
  1707. &iio_dev_attr_in_temp_high_value.dev_attr.attr,
  1708. &iio_dev_attr_in_temp_low_value.dev_attr.attr,
  1709. &iio_dev_attr_ex_temp_high_value.dev_attr.attr,
  1710. &iio_dev_attr_ex_temp_low_value.dev_attr.attr,
  1711. &iio_dev_attr_int_enabled.dev_attr.attr,
  1712. NULL,
  1713. };
  1714. static struct attribute_group adt7316_event_attribute_group = {
  1715. .attrs = adt7316_event_attributes,
  1716. };
  1717. static struct attribute *adt7516_event_attributes[] = {
  1718. &iio_dev_attr_int_mask.dev_attr.attr,
  1719. &iio_dev_attr_in_temp_high_value.dev_attr.attr,
  1720. &iio_dev_attr_in_temp_low_value.dev_attr.attr,
  1721. &iio_dev_attr_ex_temp_ain1_high_value.dev_attr.attr,
  1722. &iio_dev_attr_ex_temp_ain1_low_value.dev_attr.attr,
  1723. &iio_dev_attr_ain2_high_value.dev_attr.attr,
  1724. &iio_dev_attr_ain2_low_value.dev_attr.attr,
  1725. &iio_dev_attr_ain3_high_value.dev_attr.attr,
  1726. &iio_dev_attr_ain3_low_value.dev_attr.attr,
  1727. &iio_dev_attr_ain4_high_value.dev_attr.attr,
  1728. &iio_dev_attr_ain4_low_value.dev_attr.attr,
  1729. &iio_dev_attr_int_enabled.dev_attr.attr,
  1730. NULL,
  1731. };
  1732. static struct attribute_group adt7516_event_attribute_group = {
  1733. .attrs = adt7516_event_attributes,
  1734. };
  1735. #ifdef CONFIG_PM
  1736. int adt7316_disable(struct device *dev)
  1737. {
  1738. struct iio_dev *dev_info = dev_get_drvdata(dev);
  1739. struct adt7316_chip_info *chip = dev_info->dev_data;
  1740. return _adt7316_store_enabled(chip, 0);
  1741. }
  1742. EXPORT_SYMBOL(adt7316_disable);
  1743. int adt7316_enable(struct device *dev)
  1744. {
  1745. struct iio_dev *dev_info = dev_get_drvdata(dev);
  1746. struct adt7316_chip_info *chip = dev_info->dev_data;
  1747. return _adt7316_store_enabled(chip, 1);
  1748. }
  1749. EXPORT_SYMBOL(adt7316_enable);
  1750. #endif
  1751. static const struct iio_info adt7316_info = {
  1752. .attrs = &adt7316_attribute_group,
  1753. .num_interrupt_lines = 1,
  1754. .event_attrs = &adt7316_event_attribute_group,
  1755. .driver_module = THIS_MODULE,
  1756. };
  1757. static const struct iio_info adt7516_info = {
  1758. .attrs = &adt7516_attribute_group,
  1759. .num_interrupt_lines = 1,
  1760. .event_attrs = &adt7516_event_attribute_group,
  1761. .driver_module = THIS_MODULE,
  1762. };
  1763. /*
  1764. * device probe and remove
  1765. */
  1766. int __devinit adt7316_probe(struct device *dev, struct adt7316_bus *bus,
  1767. const char *name)
  1768. {
  1769. struct adt7316_chip_info *chip;
  1770. unsigned short *adt7316_platform_data = dev->platform_data;
  1771. int ret = 0;
  1772. chip = kzalloc(sizeof(struct adt7316_chip_info), GFP_KERNEL);
  1773. if (chip == NULL)
  1774. return -ENOMEM;
  1775. /* this is only used for device removal purposes */
  1776. dev_set_drvdata(dev, chip);
  1777. chip->bus = *bus;
  1778. if (name[4] == '3')
  1779. chip->id = ID_ADT7316 + (name[6] - '6');
  1780. else if (name[4] == '5')
  1781. chip->id = ID_ADT7516 + (name[6] - '6');
  1782. else
  1783. return -ENODEV;
  1784. chip->ldac_pin = adt7316_platform_data[1];
  1785. if (chip->ldac_pin) {
  1786. chip->config3 |= ADT7316_DA_EN_VIA_DAC_LDCA;
  1787. if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
  1788. chip->config1 |= ADT7516_SEL_AIN3;
  1789. }
  1790. chip->int_mask = ADT7316_TEMP_INT_MASK | ADT7316_VDD_INT_MASK;
  1791. if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
  1792. chip->int_mask |= ADT7516_AIN_INT_MASK;
  1793. chip->indio_dev = iio_allocate_device(0);
  1794. if (chip->indio_dev == NULL) {
  1795. ret = -ENOMEM;
  1796. goto error_free_chip;
  1797. }
  1798. chip->indio_dev->dev.parent = dev;
  1799. if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
  1800. chip->indio_dev->info = &adt7516_info;
  1801. else
  1802. chip->indio_dev->info = &adt7316_info;
  1803. chip->indio_dev->name = name;
  1804. chip->indio_dev->dev_data = (void *)chip;
  1805. chip->indio_dev->modes = INDIO_DIRECT_MODE;
  1806. ret = iio_device_register(chip->indio_dev);
  1807. if (ret)
  1808. goto error_free_dev;
  1809. if (chip->bus.irq > 0) {
  1810. if (adt7316_platform_data[0])
  1811. chip->bus.irq_flags = adt7316_platform_data[0];
  1812. ret = request_threaded_irq(chip->bus.irq,
  1813. NULL,
  1814. &adt7316_event_handler,
  1815. chip->bus.irq_flags | IRQF_ONESHOT,
  1816. chip->indio_dev->name,
  1817. chip->indio_dev);
  1818. if (ret)
  1819. goto error_unreg_dev;
  1820. if (chip->bus.irq_flags & IRQF_TRIGGER_HIGH)
  1821. chip->config1 |= ADT7316_INT_POLARITY;
  1822. }
  1823. ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG1, chip->config1);
  1824. if (ret) {
  1825. ret = -EIO;
  1826. goto error_unreg_irq;
  1827. }
  1828. ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG3, chip->config3);
  1829. if (ret) {
  1830. ret = -EIO;
  1831. goto error_unreg_irq;
  1832. }
  1833. dev_info(dev, "%s temperature sensor, ADC and DAC registered.\n",
  1834. chip->indio_dev->name);
  1835. return 0;
  1836. error_unreg_irq:
  1837. free_irq(chip->bus.irq, chip->indio_dev);
  1838. error_unreg_dev:
  1839. iio_device_unregister(chip->indio_dev);
  1840. error_free_dev:
  1841. iio_free_device(chip->indio_dev);
  1842. error_free_chip:
  1843. kfree(chip);
  1844. return ret;
  1845. }
  1846. EXPORT_SYMBOL(adt7316_probe);
  1847. int __devexit adt7316_remove(struct device *dev)
  1848. {
  1849. struct iio_dev *dev_info = dev_get_drvdata(dev);
  1850. struct adt7316_chip_info *chip = dev_info->dev_data;
  1851. dev_set_drvdata(dev, NULL);
  1852. if (chip->bus.irq)
  1853. free_irq(chip->bus.irq, chip->indio_dev);
  1854. iio_device_unregister(chip->indio_dev);
  1855. iio_free_device(chip->indio_dev);
  1856. kfree(chip);
  1857. return 0;
  1858. }
  1859. EXPORT_SYMBOL(adt7316_remove);
  1860. MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
  1861. MODULE_DESCRIPTION("Analog Devices ADT7316/7/8 and ADT7516/7/9 digital"
  1862. " temperature sensor, ADC and DAC driver");
  1863. MODULE_LICENSE("GPL v2");