max1111.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. /* assume the reference voltage to be 2.048V, with an 8-bit sample,
  92. * the LSB weight is 8mV
  93. */
  94. return sprintf(buf, "%d\n", ret * 8);
  95. }
  96. #define MAX1111_ADC_ATTR(_id) \
  97. SENSOR_DEVICE_ATTR(in##_id##_input, S_IRUGO, show_adc, NULL, _id)
  98. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  99. static MAX1111_ADC_ATTR(0);
  100. static MAX1111_ADC_ATTR(1);
  101. static MAX1111_ADC_ATTR(2);
  102. static MAX1111_ADC_ATTR(3);
  103. static struct attribute *max1111_attributes[] = {
  104. &dev_attr_name.attr,
  105. &sensor_dev_attr_in0_input.dev_attr.attr,
  106. &sensor_dev_attr_in1_input.dev_attr.attr,
  107. &sensor_dev_attr_in2_input.dev_attr.attr,
  108. &sensor_dev_attr_in3_input.dev_attr.attr,
  109. NULL,
  110. };
  111. static const struct attribute_group max1111_attr_group = {
  112. .attrs = max1111_attributes,
  113. };
  114. static int __devinit setup_transfer(struct max1111_data *data)
  115. {
  116. struct spi_message *m;
  117. struct spi_transfer *x;
  118. m = &data->msg;
  119. x = &data->xfer[0];
  120. spi_message_init(m);
  121. x->tx_buf = &data->tx_buf[0];
  122. x->len = MAX1111_TX_BUF_SIZE;
  123. spi_message_add_tail(x, m);
  124. x++;
  125. x->rx_buf = &data->rx_buf[0];
  126. x->len = MAX1111_RX_BUF_SIZE;
  127. spi_message_add_tail(x, m);
  128. return 0;
  129. }
  130. static int __devinit max1111_probe(struct spi_device *spi)
  131. {
  132. struct max1111_data *data;
  133. int err;
  134. spi->bits_per_word = 8;
  135. spi->mode = SPI_MODE_0;
  136. err = spi_setup(spi);
  137. if (err < 0)
  138. return err;
  139. data = kzalloc(sizeof(struct max1111_data), GFP_KERNEL);
  140. if (data == NULL) {
  141. dev_err(&spi->dev, "failed to allocate memory\n");
  142. return -ENOMEM;
  143. }
  144. err = setup_transfer(data);
  145. if (err)
  146. goto err_free_data;
  147. mutex_init(&data->drvdata_lock);
  148. data->spi = spi;
  149. spi_set_drvdata(spi, data);
  150. err = sysfs_create_group(&spi->dev.kobj, &max1111_attr_group);
  151. if (err) {
  152. dev_err(&spi->dev, "failed to create attribute group\n");
  153. goto err_free_data;
  154. }
  155. data->hwmon_dev = hwmon_device_register(&spi->dev);
  156. if (IS_ERR(data->hwmon_dev)) {
  157. dev_err(&spi->dev, "failed to create hwmon device\n");
  158. err = PTR_ERR(data->hwmon_dev);
  159. goto err_remove;
  160. }
  161. #ifdef CONFIG_SHARPSL_PM
  162. the_max1111 = data;
  163. #endif
  164. return 0;
  165. err_remove:
  166. sysfs_remove_group(&spi->dev.kobj, &max1111_attr_group);
  167. err_free_data:
  168. kfree(data);
  169. return err;
  170. }
  171. static int __devexit max1111_remove(struct spi_device *spi)
  172. {
  173. struct max1111_data *data = spi_get_drvdata(spi);
  174. hwmon_device_unregister(data->hwmon_dev);
  175. sysfs_remove_group(&spi->dev.kobj, &max1111_attr_group);
  176. mutex_destroy(&data->drvdata_lock);
  177. kfree(data);
  178. return 0;
  179. }
  180. static struct spi_driver max1111_driver = {
  181. .driver = {
  182. .name = "max1111",
  183. .owner = THIS_MODULE,
  184. },
  185. .probe = max1111_probe,
  186. .remove = __devexit_p(max1111_remove),
  187. };
  188. static int __init max1111_init(void)
  189. {
  190. return spi_register_driver(&max1111_driver);
  191. }
  192. module_init(max1111_init);
  193. static void __exit max1111_exit(void)
  194. {
  195. spi_unregister_driver(&max1111_driver);
  196. }
  197. module_exit(max1111_exit);
  198. MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
  199. MODULE_DESCRIPTION("MAX1111 ADC Driver");
  200. MODULE_LICENSE("GPL");
  201. MODULE_ALIAS("spi:max1111");