max1111.c 5.8 KB

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