max1111.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. #define MAX1111_TX_BUF_SIZE 1
  24. #define MAX1111_RX_BUF_SIZE 2
  25. /* MAX1111 Commands */
  26. #define MAX1111_CTRL_PD0 (1u << 0)
  27. #define MAX1111_CTRL_PD1 (1u << 1)
  28. #define MAX1111_CTRL_SGL (1u << 2)
  29. #define MAX1111_CTRL_UNI (1u << 3)
  30. #define MAX1111_CTRL_SEL_SH (5) /* NOTE: bit 4 is ignored */
  31. #define MAX1111_CTRL_STR (1u << 7)
  32. struct max1111_data {
  33. struct spi_device *spi;
  34. struct device *hwmon_dev;
  35. struct spi_message msg;
  36. struct spi_transfer xfer[2];
  37. uint8_t tx_buf[MAX1111_TX_BUF_SIZE];
  38. uint8_t rx_buf[MAX1111_RX_BUF_SIZE];
  39. struct mutex drvdata_lock;
  40. /* protect msg, xfer and buffers from multiple access */
  41. };
  42. static int max1111_read(struct device *dev, int channel)
  43. {
  44. struct max1111_data *data = dev_get_drvdata(dev);
  45. uint8_t v1, v2;
  46. int err;
  47. /* writing to drvdata struct is not thread safe, wait on mutex */
  48. mutex_lock(&data->drvdata_lock);
  49. data->tx_buf[0] = (channel << MAX1111_CTRL_SEL_SH) |
  50. MAX1111_CTRL_PD0 | MAX1111_CTRL_PD1 |
  51. MAX1111_CTRL_SGL | MAX1111_CTRL_UNI | MAX1111_CTRL_STR;
  52. err = spi_sync(data->spi, &data->msg);
  53. if (err < 0) {
  54. dev_err(dev, "spi_sync failed with %d\n", err);
  55. mutex_unlock(&data->drvdata_lock);
  56. return err;
  57. }
  58. v1 = data->rx_buf[0];
  59. v2 = data->rx_buf[1];
  60. mutex_unlock(&data->drvdata_lock);
  61. if ((v1 & 0xc0) || (v2 & 0x3f))
  62. return -EINVAL;
  63. return (v1 << 2) | (v2 >> 6);
  64. }
  65. #ifdef CONFIG_SHARPSL_PM
  66. static struct max1111_data *the_max1111;
  67. int max1111_read_channel(int channel)
  68. {
  69. return max1111_read(&the_max1111->spi->dev, channel);
  70. }
  71. EXPORT_SYMBOL(max1111_read_channel);
  72. #endif
  73. /*
  74. * NOTE: SPI devices do not have a default 'name' attribute, which is
  75. * likely to be used by hwmon applications to distinguish between
  76. * different devices, explicitly add a name attribute here.
  77. */
  78. static ssize_t show_name(struct device *dev,
  79. struct device_attribute *attr, char *buf)
  80. {
  81. return sprintf(buf, "max1111\n");
  82. }
  83. static ssize_t show_adc(struct device *dev,
  84. struct device_attribute *attr, char *buf)
  85. {
  86. int channel = to_sensor_dev_attr(attr)->index;
  87. int ret;
  88. ret = max1111_read(dev, channel);
  89. if (ret < 0)
  90. return ret;
  91. /*
  92. * assume the reference voltage to be 2.048V, with an 8-bit sample,
  93. * the LSB weight is 8mV
  94. */
  95. return sprintf(buf, "%d\n", ret * 8);
  96. }
  97. #define MAX1111_ADC_ATTR(_id) \
  98. SENSOR_DEVICE_ATTR(in##_id##_input, S_IRUGO, show_adc, NULL, _id)
  99. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  100. static MAX1111_ADC_ATTR(0);
  101. static MAX1111_ADC_ATTR(1);
  102. static MAX1111_ADC_ATTR(2);
  103. static MAX1111_ADC_ATTR(3);
  104. static struct attribute *max1111_attributes[] = {
  105. &dev_attr_name.attr,
  106. &sensor_dev_attr_in0_input.dev_attr.attr,
  107. &sensor_dev_attr_in1_input.dev_attr.attr,
  108. &sensor_dev_attr_in2_input.dev_attr.attr,
  109. &sensor_dev_attr_in3_input.dev_attr.attr,
  110. NULL,
  111. };
  112. static const struct attribute_group max1111_attr_group = {
  113. .attrs = max1111_attributes,
  114. };
  115. static int __devinit setup_transfer(struct max1111_data *data)
  116. {
  117. struct spi_message *m;
  118. struct spi_transfer *x;
  119. m = &data->msg;
  120. x = &data->xfer[0];
  121. spi_message_init(m);
  122. x->tx_buf = &data->tx_buf[0];
  123. x->len = MAX1111_TX_BUF_SIZE;
  124. spi_message_add_tail(x, m);
  125. x++;
  126. x->rx_buf = &data->rx_buf[0];
  127. x->len = MAX1111_RX_BUF_SIZE;
  128. spi_message_add_tail(x, m);
  129. return 0;
  130. }
  131. static int __devinit max1111_probe(struct spi_device *spi)
  132. {
  133. struct max1111_data *data;
  134. int err;
  135. spi->bits_per_word = 8;
  136. spi->mode = SPI_MODE_0;
  137. err = spi_setup(spi);
  138. if (err < 0)
  139. return err;
  140. data = kzalloc(sizeof(struct max1111_data), GFP_KERNEL);
  141. if (data == NULL) {
  142. dev_err(&spi->dev, "failed to allocate memory\n");
  143. return -ENOMEM;
  144. }
  145. err = setup_transfer(data);
  146. if (err)
  147. goto err_free_data;
  148. mutex_init(&data->drvdata_lock);
  149. data->spi = spi;
  150. spi_set_drvdata(spi, data);
  151. err = sysfs_create_group(&spi->dev.kobj, &max1111_attr_group);
  152. if (err) {
  153. dev_err(&spi->dev, "failed to create attribute group\n");
  154. goto err_free_data;
  155. }
  156. data->hwmon_dev = hwmon_device_register(&spi->dev);
  157. if (IS_ERR(data->hwmon_dev)) {
  158. dev_err(&spi->dev, "failed to create hwmon device\n");
  159. err = PTR_ERR(data->hwmon_dev);
  160. goto err_remove;
  161. }
  162. #ifdef CONFIG_SHARPSL_PM
  163. the_max1111 = data;
  164. #endif
  165. return 0;
  166. err_remove:
  167. sysfs_remove_group(&spi->dev.kobj, &max1111_attr_group);
  168. err_free_data:
  169. kfree(data);
  170. return err;
  171. }
  172. static int __devexit max1111_remove(struct spi_device *spi)
  173. {
  174. struct max1111_data *data = spi_get_drvdata(spi);
  175. hwmon_device_unregister(data->hwmon_dev);
  176. sysfs_remove_group(&spi->dev.kobj, &max1111_attr_group);
  177. mutex_destroy(&data->drvdata_lock);
  178. kfree(data);
  179. return 0;
  180. }
  181. static struct spi_driver max1111_driver = {
  182. .driver = {
  183. .name = "max1111",
  184. .owner = THIS_MODULE,
  185. },
  186. .probe = max1111_probe,
  187. .remove = __devexit_p(max1111_remove),
  188. };
  189. module_spi_driver(max1111_driver);
  190. MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
  191. MODULE_DESCRIPTION("MAX1111 ADC Driver");
  192. MODULE_LICENSE("GPL");
  193. MODULE_ALIAS("spi:max1111");