max1111.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. return max1111_read(&the_max1111->spi->dev, channel);
  74. }
  75. EXPORT_SYMBOL(max1111_read_channel);
  76. #endif
  77. /*
  78. * NOTE: SPI devices do not have a default 'name' attribute, which is
  79. * likely to be used by hwmon applications to distinguish between
  80. * different devices, explicitly add a name attribute here.
  81. */
  82. static ssize_t show_name(struct device *dev,
  83. struct device_attribute *attr, char *buf)
  84. {
  85. return sprintf(buf, "%s\n", to_spi_device(dev)->modalias);
  86. }
  87. static ssize_t show_adc(struct device *dev,
  88. struct device_attribute *attr, char *buf)
  89. {
  90. struct max1111_data *data = dev_get_drvdata(dev);
  91. int channel = to_sensor_dev_attr(attr)->index;
  92. int ret;
  93. ret = max1111_read(dev, channel);
  94. if (ret < 0)
  95. return ret;
  96. /*
  97. * Assume the reference voltage to be 2.048V or 4.096V, with an 8-bit
  98. * sample. The LSB weight is 8mV or 16mV depending on the chip type.
  99. */
  100. return sprintf(buf, "%d\n", ret * data->lsb);
  101. }
  102. #define MAX1111_ADC_ATTR(_id) \
  103. SENSOR_DEVICE_ATTR(in##_id##_input, S_IRUGO, show_adc, NULL, _id)
  104. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  105. static MAX1111_ADC_ATTR(0);
  106. static MAX1111_ADC_ATTR(1);
  107. static MAX1111_ADC_ATTR(2);
  108. static MAX1111_ADC_ATTR(3);
  109. static MAX1111_ADC_ATTR(4);
  110. static MAX1111_ADC_ATTR(5);
  111. static MAX1111_ADC_ATTR(6);
  112. static MAX1111_ADC_ATTR(7);
  113. static struct attribute *max1111_attributes[] = {
  114. &dev_attr_name.attr,
  115. &sensor_dev_attr_in0_input.dev_attr.attr,
  116. &sensor_dev_attr_in1_input.dev_attr.attr,
  117. &sensor_dev_attr_in2_input.dev_attr.attr,
  118. &sensor_dev_attr_in3_input.dev_attr.attr,
  119. NULL,
  120. };
  121. static const struct attribute_group max1111_attr_group = {
  122. .attrs = max1111_attributes,
  123. };
  124. static struct attribute *max1110_attributes[] = {
  125. &sensor_dev_attr_in4_input.dev_attr.attr,
  126. &sensor_dev_attr_in5_input.dev_attr.attr,
  127. &sensor_dev_attr_in6_input.dev_attr.attr,
  128. &sensor_dev_attr_in7_input.dev_attr.attr,
  129. NULL,
  130. };
  131. static const struct attribute_group max1110_attr_group = {
  132. .attrs = max1110_attributes,
  133. };
  134. static int setup_transfer(struct max1111_data *data)
  135. {
  136. struct spi_message *m;
  137. struct spi_transfer *x;
  138. m = &data->msg;
  139. x = &data->xfer[0];
  140. spi_message_init(m);
  141. x->tx_buf = &data->tx_buf[0];
  142. x->len = MAX1111_TX_BUF_SIZE;
  143. spi_message_add_tail(x, m);
  144. x++;
  145. x->rx_buf = &data->rx_buf[0];
  146. x->len = MAX1111_RX_BUF_SIZE;
  147. spi_message_add_tail(x, m);
  148. return 0;
  149. }
  150. static int max1111_probe(struct spi_device *spi)
  151. {
  152. enum chips chip = spi_get_device_id(spi)->driver_data;
  153. struct max1111_data *data;
  154. int err;
  155. spi->bits_per_word = 8;
  156. spi->mode = SPI_MODE_0;
  157. err = spi_setup(spi);
  158. if (err < 0)
  159. return err;
  160. data = devm_kzalloc(&spi->dev, sizeof(struct max1111_data), GFP_KERNEL);
  161. if (data == NULL)
  162. return -ENOMEM;
  163. switch (chip) {
  164. case max1110:
  165. data->lsb = 8;
  166. data->sel_sh = MAX1110_CTRL_SEL_SH;
  167. break;
  168. case max1111:
  169. data->lsb = 8;
  170. data->sel_sh = MAX1111_CTRL_SEL_SH;
  171. break;
  172. case max1112:
  173. data->lsb = 16;
  174. data->sel_sh = MAX1110_CTRL_SEL_SH;
  175. break;
  176. case max1113:
  177. data->lsb = 16;
  178. data->sel_sh = MAX1111_CTRL_SEL_SH;
  179. break;
  180. }
  181. err = setup_transfer(data);
  182. if (err)
  183. return err;
  184. mutex_init(&data->drvdata_lock);
  185. data->spi = spi;
  186. spi_set_drvdata(spi, data);
  187. err = sysfs_create_group(&spi->dev.kobj, &max1111_attr_group);
  188. if (err) {
  189. dev_err(&spi->dev, "failed to create attribute group\n");
  190. return err;
  191. }
  192. if (chip == max1110 || chip == max1112) {
  193. err = sysfs_create_group(&spi->dev.kobj, &max1110_attr_group);
  194. if (err) {
  195. dev_err(&spi->dev,
  196. "failed to create extended attribute group\n");
  197. goto err_remove;
  198. }
  199. }
  200. data->hwmon_dev = hwmon_device_register(&spi->dev);
  201. if (IS_ERR(data->hwmon_dev)) {
  202. dev_err(&spi->dev, "failed to create hwmon device\n");
  203. err = PTR_ERR(data->hwmon_dev);
  204. goto err_remove;
  205. }
  206. #ifdef CONFIG_SHARPSL_PM
  207. the_max1111 = data;
  208. #endif
  209. return 0;
  210. err_remove:
  211. sysfs_remove_group(&spi->dev.kobj, &max1110_attr_group);
  212. sysfs_remove_group(&spi->dev.kobj, &max1111_attr_group);
  213. return err;
  214. }
  215. static int max1111_remove(struct spi_device *spi)
  216. {
  217. struct max1111_data *data = spi_get_drvdata(spi);
  218. hwmon_device_unregister(data->hwmon_dev);
  219. sysfs_remove_group(&spi->dev.kobj, &max1110_attr_group);
  220. sysfs_remove_group(&spi->dev.kobj, &max1111_attr_group);
  221. mutex_destroy(&data->drvdata_lock);
  222. return 0;
  223. }
  224. static const struct spi_device_id max1111_ids[] = {
  225. { "max1110", max1110 },
  226. { "max1111", max1111 },
  227. { "max1112", max1112 },
  228. { "max1113", max1113 },
  229. { },
  230. };
  231. MODULE_DEVICE_TABLE(spi, max1111_ids);
  232. static struct spi_driver max1111_driver = {
  233. .driver = {
  234. .name = "max1111",
  235. .owner = THIS_MODULE,
  236. },
  237. .id_table = max1111_ids,
  238. .probe = max1111_probe,
  239. .remove = max1111_remove,
  240. };
  241. module_spi_driver(max1111_driver);
  242. MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
  243. MODULE_DESCRIPTION("MAX1110/MAX1111/MAX1112/MAX1113 ADC Driver");
  244. MODULE_LICENSE("GPL");