ads7871.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * ads7871 - driver for TI ADS7871 A/D converter
  3. *
  4. * Copyright (c) 2010 Paul Thomas <pthomas8589@gmail.com>
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 or
  13. * later as publishhed by the Free Software Foundation.
  14. *
  15. * You need to have something like this in struct spi_board_info
  16. * {
  17. * .modalias = "ads7871",
  18. * .max_speed_hz = 2*1000*1000,
  19. * .chip_select = 0,
  20. * .bus_num = 1,
  21. * },
  22. */
  23. /*From figure 18 in the datasheet*/
  24. /*Register addresses*/
  25. #define REG_LS_BYTE 0 /*A/D Output Data, LS Byte*/
  26. #define REG_MS_BYTE 1 /*A/D Output Data, MS Byte*/
  27. #define REG_PGA_VALID 2 /*PGA Valid Register*/
  28. #define REG_AD_CONTROL 3 /*A/D Control Register*/
  29. #define REG_GAIN_MUX 4 /*Gain/Mux Register*/
  30. #define REG_IO_STATE 5 /*Digital I/O State Register*/
  31. #define REG_IO_CONTROL 6 /*Digital I/O Control Register*/
  32. #define REG_OSC_CONTROL 7 /*Rev/Oscillator Control Register*/
  33. #define REG_SER_CONTROL 24 /*Serial Interface Control Register*/
  34. #define REG_ID 31 /*ID Register*/
  35. /*From figure 17 in the datasheet
  36. * These bits get ORed with the address to form
  37. * the instruction byte */
  38. /*Instruction Bit masks*/
  39. #define INST_MODE_bm (1<<7)
  40. #define INST_READ_bm (1<<6)
  41. #define INST_16BIT_bm (1<<5)
  42. /*From figure 18 in the datasheet*/
  43. /*bit masks for Rev/Oscillator Control Register*/
  44. #define MUX_CNV_bv 7
  45. #define MUX_CNV_bm (1<<MUX_CNV_bv)
  46. #define MUX_M3_bm (1<<3) /*M3 selects single ended*/
  47. #define MUX_G_bv 4 /*allows for reg = (gain << MUX_G_bv) | ...*/
  48. /*From figure 18 in the datasheet*/
  49. /*bit masks for Rev/Oscillator Control Register*/
  50. #define OSC_OSCR_bm (1<<5)
  51. #define OSC_OSCE_bm (1<<4)
  52. #define OSC_REFE_bm (1<<3)
  53. #define OSC_BUFE_bm (1<<2)
  54. #define OSC_R2V_bm (1<<1)
  55. #define OSC_RBG_bm (1<<0)
  56. #include <linux/module.h>
  57. #include <linux/init.h>
  58. #include <linux/spi/spi.h>
  59. #include <linux/hwmon.h>
  60. #include <linux/hwmon-sysfs.h>
  61. #include <linux/err.h>
  62. #include <linux/mutex.h>
  63. #include <linux/delay.h>
  64. #define DEVICE_NAME "ads7871"
  65. struct ads7871_data {
  66. struct device *hwmon_dev;
  67. struct mutex update_lock;
  68. };
  69. static int ads7871_read_reg8(struct spi_device *spi, int reg)
  70. {
  71. int ret;
  72. reg = reg | INST_READ_bm;
  73. ret = spi_w8r8(spi, reg);
  74. return ret;
  75. }
  76. static int ads7871_read_reg16(struct spi_device *spi, int reg)
  77. {
  78. int ret;
  79. reg = reg | INST_READ_bm | INST_16BIT_bm;
  80. ret = spi_w8r16(spi, reg);
  81. return ret;
  82. }
  83. static int ads7871_write_reg8(struct spi_device *spi, int reg, u8 val)
  84. {
  85. u8 tmp[2] = {reg, val};
  86. return spi_write(spi, tmp, sizeof(tmp));
  87. }
  88. static ssize_t show_voltage(struct device *dev,
  89. struct device_attribute *da, char *buf)
  90. {
  91. struct spi_device *spi = to_spi_device(dev);
  92. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  93. int ret, val, i = 0;
  94. uint8_t channel, mux_cnv;
  95. channel = attr->index;
  96. /*TODO: add support for conversions
  97. *other than single ended with a gain of 1*/
  98. /*MUX_M3_bm forces single ended*/
  99. /*This is also where the gain of the PGA would be set*/
  100. ads7871_write_reg8(spi, REG_GAIN_MUX,
  101. (MUX_CNV_bm | MUX_M3_bm | channel));
  102. ret = ads7871_read_reg8(spi, REG_GAIN_MUX);
  103. mux_cnv = ((ret & MUX_CNV_bm)>>MUX_CNV_bv);
  104. /*on 400MHz arm9 platform the conversion
  105. *is already done when we do this test*/
  106. while ((i < 2) && mux_cnv) {
  107. i++;
  108. ret = ads7871_read_reg8(spi, REG_GAIN_MUX);
  109. mux_cnv = ((ret & MUX_CNV_bm)>>MUX_CNV_bv);
  110. msleep_interruptible(1);
  111. }
  112. if (mux_cnv == 0) {
  113. val = ads7871_read_reg16(spi, REG_LS_BYTE);
  114. /*result in volts*10000 = (val/8192)*2.5*10000*/
  115. val = ((val>>2) * 25000) / 8192;
  116. return sprintf(buf, "%d\n", val);
  117. } else {
  118. return -1;
  119. }
  120. }
  121. static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_voltage, NULL, 0);
  122. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_voltage, NULL, 1);
  123. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_voltage, NULL, 2);
  124. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_voltage, NULL, 3);
  125. static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_voltage, NULL, 4);
  126. static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_voltage, NULL, 5);
  127. static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_voltage, NULL, 6);
  128. static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, show_voltage, NULL, 7);
  129. static struct attribute *ads7871_attributes[] = {
  130. &sensor_dev_attr_in0_input.dev_attr.attr,
  131. &sensor_dev_attr_in1_input.dev_attr.attr,
  132. &sensor_dev_attr_in2_input.dev_attr.attr,
  133. &sensor_dev_attr_in3_input.dev_attr.attr,
  134. &sensor_dev_attr_in4_input.dev_attr.attr,
  135. &sensor_dev_attr_in5_input.dev_attr.attr,
  136. &sensor_dev_attr_in6_input.dev_attr.attr,
  137. &sensor_dev_attr_in7_input.dev_attr.attr,
  138. NULL
  139. };
  140. static const struct attribute_group ads7871_group = {
  141. .attrs = ads7871_attributes,
  142. };
  143. static int __devinit ads7871_probe(struct spi_device *spi)
  144. {
  145. int ret, err;
  146. uint8_t val;
  147. struct ads7871_data *pdata;
  148. dev_dbg(&spi->dev, "probe\n");
  149. /* Configure the SPI bus */
  150. spi->mode = (SPI_MODE_0);
  151. spi->bits_per_word = 8;
  152. spi_setup(spi);
  153. ads7871_write_reg8(spi, REG_SER_CONTROL, 0);
  154. ads7871_write_reg8(spi, REG_AD_CONTROL, 0);
  155. val = (OSC_OSCR_bm | OSC_OSCE_bm | OSC_REFE_bm | OSC_BUFE_bm);
  156. ads7871_write_reg8(spi, REG_OSC_CONTROL, val);
  157. ret = ads7871_read_reg8(spi, REG_OSC_CONTROL);
  158. dev_dbg(&spi->dev, "REG_OSC_CONTROL write:%x, read:%x\n", val, ret);
  159. /*because there is no other error checking on an SPI bus
  160. we need to make sure we really have a chip*/
  161. if (val != ret) {
  162. err = -ENODEV;
  163. goto exit;
  164. }
  165. pdata = kzalloc(sizeof(struct ads7871_data), GFP_KERNEL);
  166. if (!pdata) {
  167. err = -ENOMEM;
  168. goto exit;
  169. }
  170. err = sysfs_create_group(&spi->dev.kobj, &ads7871_group);
  171. if (err < 0)
  172. goto error_free;
  173. spi_set_drvdata(spi, pdata);
  174. pdata->hwmon_dev = hwmon_device_register(&spi->dev);
  175. if (IS_ERR(pdata->hwmon_dev)) {
  176. err = PTR_ERR(pdata->hwmon_dev);
  177. goto error_remove;
  178. }
  179. return 0;
  180. error_remove:
  181. sysfs_remove_group(&spi->dev.kobj, &ads7871_group);
  182. error_free:
  183. kfree(pdata);
  184. exit:
  185. return err;
  186. }
  187. static int __devexit ads7871_remove(struct spi_device *spi)
  188. {
  189. struct ads7871_data *pdata = spi_get_drvdata(spi);
  190. hwmon_device_unregister(pdata->hwmon_dev);
  191. sysfs_remove_group(&spi->dev.kobj, &ads7871_group);
  192. kfree(pdata);
  193. return 0;
  194. }
  195. static struct spi_driver ads7871_driver = {
  196. .driver = {
  197. .name = DEVICE_NAME,
  198. .bus = &spi_bus_type,
  199. .owner = THIS_MODULE,
  200. },
  201. .probe = ads7871_probe,
  202. .remove = __devexit_p(ads7871_remove),
  203. };
  204. static int __init ads7871_init(void)
  205. {
  206. return spi_register_driver(&ads7871_driver);
  207. }
  208. static void __exit ads7871_exit(void)
  209. {
  210. spi_unregister_driver(&ads7871_driver);
  211. }
  212. module_init(ads7871_init);
  213. module_exit(ads7871_exit);
  214. MODULE_AUTHOR("Paul Thomas <pthomas8589@gmail.com>");
  215. MODULE_DESCRIPTION("TI ADS7871 A/D driver");
  216. MODULE_LICENSE("GPL");