max1111.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * max1111.c - +2.7V, Low-Power, Multichannel, Serial 8-bit ADCs
  3. *
  4. * Based on arch/arm/mach-pxa/corgi_ssp.c
  5. *
  6. * Copyright (C) 2004-2005 Richard Purdie
  7. *
  8. * Copyright (C) 2008 Marvell International Ltd.
  9. * Eric Miao <eric.miao@marvell.com>
  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 as
  13. * publishhed by the Free Software Foundation.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/init.h>
  18. #include <linux/err.h>
  19. #include <linux/hwmon.h>
  20. #include <linux/hwmon-sysfs.h>
  21. #include <linux/spi/spi.h>
  22. #include <linux/slab.h>
  23. enum chips { max1110, max1111, max1112, max1113 };
  24. #define MAX1111_TX_BUF_SIZE 1
  25. #define MAX1111_RX_BUF_SIZE 2
  26. /* MAX1111 Commands */
  27. #define MAX1111_CTRL_PD0 (1u << 0)
  28. #define MAX1111_CTRL_PD1 (1u << 1)
  29. #define MAX1111_CTRL_SGL (1u << 2)
  30. #define MAX1111_CTRL_UNI (1u << 3)
  31. #define MAX1110_CTRL_SEL_SH (4)
  32. #define MAX1111_CTRL_SEL_SH (5) /* NOTE: bit 4 is ignored */
  33. #define MAX1111_CTRL_STR (1u << 7)
  34. struct max1111_data {
  35. struct spi_device *spi;
  36. struct device *hwmon_dev;
  37. struct spi_message msg;
  38. struct spi_transfer xfer[2];
  39. uint8_t tx_buf[MAX1111_TX_BUF_SIZE];
  40. uint8_t rx_buf[MAX1111_RX_BUF_SIZE];
  41. struct mutex drvdata_lock;
  42. /* protect msg, xfer and buffers from multiple access */
  43. int sel_sh;
  44. int lsb;
  45. };
  46. static int max1111_read(struct device *dev, int channel)
  47. {
  48. struct max1111_data *data = dev_get_drvdata(dev);
  49. uint8_t v1, v2;
  50. int err;
  51. /* writing to drvdata struct is not thread safe, wait on mutex */
  52. mutex_lock(&data->drvdata_lock);
  53. data->tx_buf[0] = (channel << data->sel_sh) |
  54. MAX1111_CTRL_PD0 | MAX1111_CTRL_PD1 |
  55. MAX1111_CTRL_SGL | MAX1111_CTRL_UNI | MAX1111_CTRL_STR;
  56. err = spi_sync(data->spi, &data->msg);
  57. if (err < 0) {
  58. dev_err(dev, "spi_sync failed with %d\n", err);
  59. mutex_unlock(&data->drvdata_lock);
  60. return err;
  61. }
  62. v1 = data->rx_buf[0];
  63. v2 = data->rx_buf[1];
  64. mutex_unlock(&data->drvdata_lock);
  65. if ((v1 & 0xc0) || (v2 & 0x3f))
  66. return -EINVAL;
  67. return (v1 << 2) | (v2 >> 6);
  68. }
  69. #ifdef CONFIG_SHARPSL_PM
  70. static struct max1111_data *the_max1111;
  71. int max1111_read_channel(int channel)
  72. {
  73. if (!the_max1111 || !the_max1111->spi)
  74. return -ENODEV;
  75. return max1111_read(&the_max1111->spi->dev, channel);
  76. }
  77. EXPORT_SYMBOL(max1111_read_channel);
  78. #endif
  79. /*
  80. * NOTE: SPI devices do not have a default 'name' attribute, which is
  81. * likely to be used by hwmon applications to distinguish between
  82. * different devices, explicitly add a name attribute here.
  83. */
  84. static ssize_t show_name(struct device *dev,
  85. struct device_attribute *attr, char *buf)
  86. {
  87. return sprintf(buf, "%s\n", to_spi_device(dev)->modalias);
  88. }
  89. static ssize_t show_adc(struct device *dev,
  90. struct device_attribute *attr, char *buf)
  91. {
  92. struct max1111_data *data = dev_get_drvdata(dev);
  93. int channel = to_sensor_dev_attr(attr)->index;
  94. int ret;
  95. ret = max1111_read(dev, channel);
  96. if (ret < 0)
  97. return ret;
  98. /*
  99. * Assume the reference voltage to be 2.048V or 4.096V, with an 8-bit
  100. * sample. The LSB weight is 8mV or 16mV depending on the chip type.
  101. */
  102. return sprintf(buf, "%d\n", ret * data->lsb);
  103. }
  104. #define MAX1111_ADC_ATTR(_id) \
  105. SENSOR_DEVICE_ATTR(in##_id##_input, S_IRUGO, show_adc, NULL, _id)
  106. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  107. static MAX1111_ADC_ATTR(0);
  108. static MAX1111_ADC_ATTR(1);
  109. static MAX1111_ADC_ATTR(2);
  110. static MAX1111_ADC_ATTR(3);
  111. static MAX1111_ADC_ATTR(4);
  112. static MAX1111_ADC_ATTR(5);
  113. static MAX1111_ADC_ATTR(6);
  114. static MAX1111_ADC_ATTR(7);
  115. static struct attribute *max1111_attributes[] = {
  116. &dev_attr_name.attr,
  117. &sensor_dev_attr_in0_input.dev_attr.attr,
  118. &sensor_dev_attr_in1_input.dev_attr.attr,
  119. &sensor_dev_attr_in2_input.dev_attr.attr,
  120. &sensor_dev_attr_in3_input.dev_attr.attr,
  121. NULL,
  122. };
  123. static const struct attribute_group max1111_attr_group = {
  124. .attrs = max1111_attributes,
  125. };
  126. static struct attribute *max1110_attributes[] = {
  127. &sensor_dev_attr_in4_input.dev_attr.attr,
  128. &sensor_dev_attr_in5_input.dev_attr.attr,
  129. &sensor_dev_attr_in6_input.dev_attr.attr,
  130. &sensor_dev_attr_in7_input.dev_attr.attr,
  131. NULL,
  132. };
  133. static const struct attribute_group max1110_attr_group = {
  134. .attrs = max1110_attributes,
  135. };
  136. static int setup_transfer(struct max1111_data *data)
  137. {
  138. struct spi_message *m;
  139. struct spi_transfer *x;
  140. m = &data->msg;
  141. x = &data->xfer[0];
  142. spi_message_init(m);
  143. x->tx_buf = &data->tx_buf[0];
  144. x->len = MAX1111_TX_BUF_SIZE;
  145. spi_message_add_tail(x, m);
  146. x++;
  147. x->rx_buf = &data->rx_buf[0];
  148. x->len = MAX1111_RX_BUF_SIZE;
  149. spi_message_add_tail(x, m);
  150. return 0;
  151. }
  152. static int max1111_probe(struct spi_device *spi)
  153. {
  154. enum chips chip = spi_get_device_id(spi)->driver_data;
  155. struct max1111_data *data;
  156. int err;
  157. spi->bits_per_word = 8;
  158. spi->mode = SPI_MODE_0;
  159. err = spi_setup(spi);
  160. if (err < 0)
  161. return err;
  162. data = devm_kzalloc(&spi->dev, sizeof(struct max1111_data), GFP_KERNEL);
  163. if (data == NULL)
  164. return -ENOMEM;
  165. switch (chip) {
  166. case max1110:
  167. data->lsb = 8;
  168. data->sel_sh = MAX1110_CTRL_SEL_SH;
  169. break;
  170. case max1111:
  171. data->lsb = 8;
  172. data->sel_sh = MAX1111_CTRL_SEL_SH;
  173. break;
  174. case max1112:
  175. data->lsb = 16;
  176. data->sel_sh = MAX1110_CTRL_SEL_SH;
  177. break;
  178. case max1113:
  179. data->lsb = 16;
  180. data->sel_sh = MAX1111_CTRL_SEL_SH;
  181. break;
  182. }
  183. err = setup_transfer(data);
  184. if (err)
  185. return err;
  186. mutex_init(&data->drvdata_lock);
  187. data->spi = spi;
  188. spi_set_drvdata(spi, data);
  189. err = sysfs_create_group(&spi->dev.kobj, &max1111_attr_group);
  190. if (err) {
  191. dev_err(&spi->dev, "failed to create attribute group\n");
  192. return err;
  193. }
  194. if (chip == max1110 || chip == max1112) {
  195. err = sysfs_create_group(&spi->dev.kobj, &max1110_attr_group);
  196. if (err) {
  197. dev_err(&spi->dev,
  198. "failed to create extended attribute group\n");
  199. goto err_remove;
  200. }
  201. }
  202. data->hwmon_dev = hwmon_device_register(&spi->dev);
  203. if (IS_ERR(data->hwmon_dev)) {
  204. dev_err(&spi->dev, "failed to create hwmon device\n");
  205. err = PTR_ERR(data->hwmon_dev);
  206. goto err_remove;
  207. }
  208. #ifdef CONFIG_SHARPSL_PM
  209. the_max1111 = data;
  210. #endif
  211. return 0;
  212. err_remove:
  213. sysfs_remove_group(&spi->dev.kobj, &max1110_attr_group);
  214. sysfs_remove_group(&spi->dev.kobj, &max1111_attr_group);
  215. return err;
  216. }
  217. static int max1111_remove(struct spi_device *spi)
  218. {
  219. struct max1111_data *data = spi_get_drvdata(spi);
  220. #ifdef CONFIG_SHARPSL_PM
  221. the_max1111 = NULL;
  222. #endif
  223. hwmon_device_unregister(data->hwmon_dev);
  224. sysfs_remove_group(&spi->dev.kobj, &max1110_attr_group);
  225. sysfs_remove_group(&spi->dev.kobj, &max1111_attr_group);
  226. mutex_destroy(&data->drvdata_lock);
  227. return 0;
  228. }
  229. static const struct spi_device_id max1111_ids[] = {
  230. { "max1110", max1110 },
  231. { "max1111", max1111 },
  232. { "max1112", max1112 },
  233. { "max1113", max1113 },
  234. { },
  235. };
  236. MODULE_DEVICE_TABLE(spi, max1111_ids);
  237. static struct spi_driver max1111_driver = {
  238. .driver = {
  239. .name = "max1111",
  240. },
  241. .id_table = max1111_ids,
  242. .probe = max1111_probe,
  243. .remove = max1111_remove,
  244. };
  245. module_spi_driver(max1111_driver);
  246. MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
  247. MODULE_DESCRIPTION("MAX1110/MAX1111/MAX1112/MAX1113 ADC Driver");
  248. MODULE_LICENSE("GPL");