ads7871.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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/delay.h>
  65. #define DEVICE_NAME "ads7871"
  66. struct ads7871_data {
  67. struct spi_device *spi;
  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 ads7871_data *pdata = dev_get_drvdata(dev);
  92. struct spi_device *spi = pdata->spi;
  93. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  94. int ret, val, i = 0;
  95. uint8_t channel, mux_cnv;
  96. channel = attr->index;
  97. /*
  98. * TODO: add support for conversions
  99. * other than single ended with a gain of 1
  100. */
  101. /*MUX_M3_BM forces single ended*/
  102. /*This is also where the gain of the PGA would be set*/
  103. ads7871_write_reg8(spi, REG_GAIN_MUX,
  104. (MUX_CNV_BM | MUX_M3_BM | channel));
  105. ret = ads7871_read_reg8(spi, REG_GAIN_MUX);
  106. mux_cnv = ((ret & MUX_CNV_BM) >> MUX_CNV_BV);
  107. /*
  108. * on 400MHz arm9 platform the conversion
  109. * is already done when we do this test
  110. */
  111. while ((i < 2) && mux_cnv) {
  112. i++;
  113. ret = ads7871_read_reg8(spi, REG_GAIN_MUX);
  114. mux_cnv = ((ret & MUX_CNV_BM) >> MUX_CNV_BV);
  115. msleep_interruptible(1);
  116. }
  117. if (mux_cnv == 0) {
  118. val = ads7871_read_reg16(spi, REG_LS_BYTE);
  119. /*result in volts*10000 = (val/8192)*2.5*10000*/
  120. val = ((val >> 2) * 25000) / 8192;
  121. return sprintf(buf, "%d\n", val);
  122. } else {
  123. return -1;
  124. }
  125. }
  126. static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_voltage, NULL, 0);
  127. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_voltage, NULL, 1);
  128. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_voltage, NULL, 2);
  129. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_voltage, NULL, 3);
  130. static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_voltage, NULL, 4);
  131. static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_voltage, NULL, 5);
  132. static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_voltage, NULL, 6);
  133. static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, show_voltage, NULL, 7);
  134. static struct attribute *ads7871_attrs[] = {
  135. &sensor_dev_attr_in0_input.dev_attr.attr,
  136. &sensor_dev_attr_in1_input.dev_attr.attr,
  137. &sensor_dev_attr_in2_input.dev_attr.attr,
  138. &sensor_dev_attr_in3_input.dev_attr.attr,
  139. &sensor_dev_attr_in4_input.dev_attr.attr,
  140. &sensor_dev_attr_in5_input.dev_attr.attr,
  141. &sensor_dev_attr_in6_input.dev_attr.attr,
  142. &sensor_dev_attr_in7_input.dev_attr.attr,
  143. NULL
  144. };
  145. ATTRIBUTE_GROUPS(ads7871);
  146. static int ads7871_probe(struct spi_device *spi)
  147. {
  148. struct device *dev = &spi->dev;
  149. int ret;
  150. uint8_t val;
  151. struct ads7871_data *pdata;
  152. struct device *hwmon_dev;
  153. /* Configure the SPI bus */
  154. spi->mode = (SPI_MODE_0);
  155. spi->bits_per_word = 8;
  156. spi_setup(spi);
  157. ads7871_write_reg8(spi, REG_SER_CONTROL, 0);
  158. ads7871_write_reg8(spi, REG_AD_CONTROL, 0);
  159. val = (OSC_OSCR_BM | OSC_OSCE_BM | OSC_REFE_BM | OSC_BUFE_BM);
  160. ads7871_write_reg8(spi, REG_OSC_CONTROL, val);
  161. ret = ads7871_read_reg8(spi, REG_OSC_CONTROL);
  162. dev_dbg(dev, "REG_OSC_CONTROL write:%x, read:%x\n", val, ret);
  163. /*
  164. * because there is no other error checking on an SPI bus
  165. * we need to make sure we really have a chip
  166. */
  167. if (val != ret)
  168. return -ENODEV;
  169. pdata = devm_kzalloc(dev, sizeof(struct ads7871_data), GFP_KERNEL);
  170. if (!pdata)
  171. return -ENOMEM;
  172. pdata->spi = spi;
  173. hwmon_dev = devm_hwmon_device_register_with_groups(dev, spi->modalias,
  174. pdata,
  175. ads7871_groups);
  176. return PTR_ERR_OR_ZERO(hwmon_dev);
  177. }
  178. static struct spi_driver ads7871_driver = {
  179. .driver = {
  180. .name = DEVICE_NAME,
  181. },
  182. .probe = ads7871_probe,
  183. };
  184. module_spi_driver(ads7871_driver);
  185. MODULE_AUTHOR("Paul Thomas <pthomas8589@gmail.com>");
  186. MODULE_DESCRIPTION("TI ADS7871 A/D driver");
  187. MODULE_LICENSE("GPL");