ads7871.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. /*
  36. * From figure 17 in the datasheet
  37. * These bits get ORed with the address to form
  38. * the instruction byte
  39. */
  40. /*Instruction Bit masks*/
  41. #define INST_MODE_BM (1 << 7)
  42. #define INST_READ_BM (1 << 6)
  43. #define INST_16BIT_BM (1 << 5)
  44. /*From figure 18 in the datasheet*/
  45. /*bit masks for Rev/Oscillator Control Register*/
  46. #define MUX_CNV_BV 7
  47. #define MUX_CNV_BM (1 << MUX_CNV_BV)
  48. #define MUX_M3_BM (1 << 3) /*M3 selects single ended*/
  49. #define MUX_G_BV 4 /*allows for reg = (gain << MUX_G_BV) | ...*/
  50. /*From figure 18 in the datasheet*/
  51. /*bit masks for Rev/Oscillator Control Register*/
  52. #define OSC_OSCR_BM (1 << 5)
  53. #define OSC_OSCE_BM (1 << 4)
  54. #define OSC_REFE_BM (1 << 3)
  55. #define OSC_BUFE_BM (1 << 2)
  56. #define OSC_R2V_BM (1 << 1)
  57. #define OSC_RBG_BM (1 << 0)
  58. #include <linux/module.h>
  59. #include <linux/init.h>
  60. #include <linux/spi/spi.h>
  61. #include <linux/hwmon.h>
  62. #include <linux/hwmon-sysfs.h>
  63. #include <linux/err.h>
  64. #include <linux/mutex.h>
  65. #include <linux/delay.h>
  66. #define DEVICE_NAME "ads7871"
  67. struct ads7871_data {
  68. struct device *hwmon_dev;
  69. struct mutex update_lock;
  70. };
  71. static int ads7871_read_reg8(struct spi_device *spi, int reg)
  72. {
  73. int ret;
  74. reg = reg | INST_READ_BM;
  75. ret = spi_w8r8(spi, reg);
  76. return ret;
  77. }
  78. static int ads7871_read_reg16(struct spi_device *spi, int reg)
  79. {
  80. int ret;
  81. reg = reg | INST_READ_BM | INST_16BIT_BM;
  82. ret = spi_w8r16(spi, reg);
  83. return ret;
  84. }
  85. static int ads7871_write_reg8(struct spi_device *spi, int reg, u8 val)
  86. {
  87. u8 tmp[2] = {reg, val};
  88. return spi_write(spi, tmp, sizeof(tmp));
  89. }
  90. static ssize_t show_voltage(struct device *dev,
  91. struct device_attribute *da, char *buf)
  92. {
  93. struct spi_device *spi = to_spi_device(dev);
  94. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  95. int ret, val, i = 0;
  96. uint8_t channel, mux_cnv;
  97. channel = attr->index;
  98. /*
  99. * TODO: add support for conversions
  100. * other than single ended with a gain of 1
  101. */
  102. /*MUX_M3_BM forces single ended*/
  103. /*This is also where the gain of the PGA would be set*/
  104. ads7871_write_reg8(spi, REG_GAIN_MUX,
  105. (MUX_CNV_BM | MUX_M3_BM | channel));
  106. ret = ads7871_read_reg8(spi, REG_GAIN_MUX);
  107. mux_cnv = ((ret & MUX_CNV_BM) >> MUX_CNV_BV);
  108. /*
  109. * on 400MHz arm9 platform the conversion
  110. * is already done when we do this test
  111. */
  112. while ((i < 2) && mux_cnv) {
  113. i++;
  114. ret = ads7871_read_reg8(spi, REG_GAIN_MUX);
  115. mux_cnv = ((ret & MUX_CNV_BM) >> MUX_CNV_BV);
  116. msleep_interruptible(1);
  117. }
  118. if (mux_cnv == 0) {
  119. val = ads7871_read_reg16(spi, REG_LS_BYTE);
  120. /*result in volts*10000 = (val/8192)*2.5*10000*/
  121. val = ((val >> 2) * 25000) / 8192;
  122. return sprintf(buf, "%d\n", val);
  123. } else {
  124. return -1;
  125. }
  126. }
  127. static ssize_t ads7871_show_name(struct device *dev,
  128. struct device_attribute *devattr, char *buf)
  129. {
  130. return sprintf(buf, "%s\n", to_spi_device(dev)->modalias);
  131. }
  132. static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_voltage, NULL, 0);
  133. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_voltage, NULL, 1);
  134. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_voltage, NULL, 2);
  135. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_voltage, NULL, 3);
  136. static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_voltage, NULL, 4);
  137. static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_voltage, NULL, 5);
  138. static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_voltage, NULL, 6);
  139. static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, show_voltage, NULL, 7);
  140. static DEVICE_ATTR(name, S_IRUGO, ads7871_show_name, NULL);
  141. static struct attribute *ads7871_attributes[] = {
  142. &sensor_dev_attr_in0_input.dev_attr.attr,
  143. &sensor_dev_attr_in1_input.dev_attr.attr,
  144. &sensor_dev_attr_in2_input.dev_attr.attr,
  145. &sensor_dev_attr_in3_input.dev_attr.attr,
  146. &sensor_dev_attr_in4_input.dev_attr.attr,
  147. &sensor_dev_attr_in5_input.dev_attr.attr,
  148. &sensor_dev_attr_in6_input.dev_attr.attr,
  149. &sensor_dev_attr_in7_input.dev_attr.attr,
  150. &dev_attr_name.attr,
  151. NULL
  152. };
  153. static const struct attribute_group ads7871_group = {
  154. .attrs = ads7871_attributes,
  155. };
  156. static int ads7871_probe(struct spi_device *spi)
  157. {
  158. int ret, err;
  159. uint8_t val;
  160. struct ads7871_data *pdata;
  161. dev_dbg(&spi->dev, "probe\n");
  162. /* Configure the SPI bus */
  163. spi->mode = (SPI_MODE_0);
  164. spi->bits_per_word = 8;
  165. spi_setup(spi);
  166. ads7871_write_reg8(spi, REG_SER_CONTROL, 0);
  167. ads7871_write_reg8(spi, REG_AD_CONTROL, 0);
  168. val = (OSC_OSCR_BM | OSC_OSCE_BM | OSC_REFE_BM | OSC_BUFE_BM);
  169. ads7871_write_reg8(spi, REG_OSC_CONTROL, val);
  170. ret = ads7871_read_reg8(spi, REG_OSC_CONTROL);
  171. dev_dbg(&spi->dev, "REG_OSC_CONTROL write:%x, read:%x\n", val, ret);
  172. /*
  173. * because there is no other error checking on an SPI bus
  174. * we need to make sure we really have a chip
  175. */
  176. if (val != ret)
  177. return -ENODEV;
  178. pdata = devm_kzalloc(&spi->dev, sizeof(struct ads7871_data),
  179. GFP_KERNEL);
  180. if (!pdata)
  181. return -ENOMEM;
  182. err = sysfs_create_group(&spi->dev.kobj, &ads7871_group);
  183. if (err < 0)
  184. return err;
  185. spi_set_drvdata(spi, pdata);
  186. pdata->hwmon_dev = hwmon_device_register(&spi->dev);
  187. if (IS_ERR(pdata->hwmon_dev)) {
  188. err = PTR_ERR(pdata->hwmon_dev);
  189. goto error_remove;
  190. }
  191. return 0;
  192. error_remove:
  193. sysfs_remove_group(&spi->dev.kobj, &ads7871_group);
  194. return err;
  195. }
  196. static int ads7871_remove(struct spi_device *spi)
  197. {
  198. struct ads7871_data *pdata = spi_get_drvdata(spi);
  199. hwmon_device_unregister(pdata->hwmon_dev);
  200. sysfs_remove_group(&spi->dev.kobj, &ads7871_group);
  201. return 0;
  202. }
  203. static struct spi_driver ads7871_driver = {
  204. .driver = {
  205. .name = DEVICE_NAME,
  206. },
  207. .probe = ads7871_probe,
  208. .remove = ads7871_remove,
  209. };
  210. module_spi_driver(ads7871_driver);
  211. MODULE_AUTHOR("Paul Thomas <pthomas8589@gmail.com>");
  212. MODULE_DESCRIPTION("TI ADS7871 A/D driver");
  213. MODULE_LICENSE("GPL");