mc13783-adc.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Driver for the ADC on Freescale Semiconductor MC13783 and MC13892 PMICs.
  3. *
  4. * Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved.
  5. * Copyright (C) 2009 Sascha Hauer, Pengutronix
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program; if not, write to the Free Software Foundation, Inc., 51
  18. * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <linux/mfd/mc13xxx.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/hwmon-sysfs.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/hwmon.h>
  26. #include <linux/slab.h>
  27. #include <linux/init.h>
  28. #include <linux/err.h>
  29. #define DRIVER_NAME "mc13783-adc"
  30. /* platform device id driver data */
  31. #define MC13783_ADC_16CHANS 1
  32. #define MC13783_ADC_BPDIV2 2
  33. struct mc13783_adc_priv {
  34. struct mc13xxx *mc13xxx;
  35. struct device *hwmon_dev;
  36. char name[PLATFORM_NAME_SIZE];
  37. };
  38. static ssize_t mc13783_adc_show_name(struct device *dev, struct device_attribute
  39. *devattr, char *buf)
  40. {
  41. struct mc13783_adc_priv *priv = dev_get_drvdata(dev);
  42. return sprintf(buf, "%s\n", priv->name);
  43. }
  44. static int mc13783_adc_read(struct device *dev,
  45. struct device_attribute *devattr, unsigned int *val)
  46. {
  47. struct mc13783_adc_priv *priv = dev_get_drvdata(dev);
  48. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  49. unsigned int channel = attr->index;
  50. unsigned int sample[4];
  51. int ret;
  52. ret = mc13xxx_adc_do_conversion(priv->mc13xxx,
  53. MC13XXX_ADC_MODE_MULT_CHAN,
  54. channel, 0, 0, sample);
  55. if (ret)
  56. return ret;
  57. channel &= 0x7;
  58. *val = (sample[channel % 4] >> (channel > 3 ? 14 : 2)) & 0x3ff;
  59. return 0;
  60. }
  61. static ssize_t mc13783_adc_read_bp(struct device *dev,
  62. struct device_attribute *devattr, char *buf)
  63. {
  64. unsigned val;
  65. struct platform_device *pdev = to_platform_device(dev);
  66. kernel_ulong_t driver_data = platform_get_device_id(pdev)->driver_data;
  67. int ret = mc13783_adc_read(dev, devattr, &val);
  68. if (ret)
  69. return ret;
  70. if (driver_data & MC13783_ADC_BPDIV2)
  71. val = DIV_ROUND_CLOSEST(val * 9, 2);
  72. else
  73. /*
  74. * BP (channel 2) reports with offset 2.4V to the actual value
  75. * to fit the input range of the ADC. unit = 2.25mV = 9/4 mV.
  76. */
  77. val = DIV_ROUND_CLOSEST(val * 9, 4) + 2400;
  78. return sprintf(buf, "%u\n", val);
  79. }
  80. static ssize_t mc13783_adc_read_gp(struct device *dev,
  81. struct device_attribute *devattr, char *buf)
  82. {
  83. unsigned val;
  84. int ret = mc13783_adc_read(dev, devattr, &val);
  85. if (ret)
  86. return ret;
  87. /*
  88. * input range is [0, 2.3V], val has 10 bits, so each bit
  89. * is worth 9/4 mV.
  90. */
  91. val = DIV_ROUND_CLOSEST(val * 9, 4);
  92. return sprintf(buf, "%u\n", val);
  93. }
  94. static DEVICE_ATTR(name, S_IRUGO, mc13783_adc_show_name, NULL);
  95. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, mc13783_adc_read_bp, NULL, 2);
  96. static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, mc13783_adc_read_gp, NULL, 5);
  97. static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, mc13783_adc_read_gp, NULL, 6);
  98. static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, mc13783_adc_read_gp, NULL, 7);
  99. static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, mc13783_adc_read_gp, NULL, 8);
  100. static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, mc13783_adc_read_gp, NULL, 9);
  101. static SENSOR_DEVICE_ATTR(in10_input, S_IRUGO, mc13783_adc_read_gp, NULL, 10);
  102. static SENSOR_DEVICE_ATTR(in11_input, S_IRUGO, mc13783_adc_read_gp, NULL, 11);
  103. static SENSOR_DEVICE_ATTR(in12_input, S_IRUGO, mc13783_adc_read_gp, NULL, 12);
  104. static SENSOR_DEVICE_ATTR(in13_input, S_IRUGO, mc13783_adc_read_gp, NULL, 13);
  105. static SENSOR_DEVICE_ATTR(in14_input, S_IRUGO, mc13783_adc_read_gp, NULL, 14);
  106. static SENSOR_DEVICE_ATTR(in15_input, S_IRUGO, mc13783_adc_read_gp, NULL, 15);
  107. static struct attribute *mc13783_attr_base[] = {
  108. &dev_attr_name.attr,
  109. &sensor_dev_attr_in2_input.dev_attr.attr,
  110. &sensor_dev_attr_in5_input.dev_attr.attr,
  111. &sensor_dev_attr_in6_input.dev_attr.attr,
  112. &sensor_dev_attr_in7_input.dev_attr.attr,
  113. NULL
  114. };
  115. static const struct attribute_group mc13783_group_base = {
  116. .attrs = mc13783_attr_base,
  117. };
  118. /* these are only used if MC13783_ADC_16CHANS is provided in driver data */
  119. static struct attribute *mc13783_attr_16chans[] = {
  120. &sensor_dev_attr_in8_input.dev_attr.attr,
  121. &sensor_dev_attr_in9_input.dev_attr.attr,
  122. &sensor_dev_attr_in10_input.dev_attr.attr,
  123. &sensor_dev_attr_in11_input.dev_attr.attr,
  124. NULL
  125. };
  126. static const struct attribute_group mc13783_group_16chans = {
  127. .attrs = mc13783_attr_16chans,
  128. };
  129. /* last four channels may be occupied by the touchscreen */
  130. static struct attribute *mc13783_attr_ts[] = {
  131. &sensor_dev_attr_in12_input.dev_attr.attr,
  132. &sensor_dev_attr_in13_input.dev_attr.attr,
  133. &sensor_dev_attr_in14_input.dev_attr.attr,
  134. &sensor_dev_attr_in15_input.dev_attr.attr,
  135. NULL
  136. };
  137. static const struct attribute_group mc13783_group_ts = {
  138. .attrs = mc13783_attr_ts,
  139. };
  140. static int mc13783_adc_use_touchscreen(struct platform_device *pdev)
  141. {
  142. struct mc13783_adc_priv *priv = platform_get_drvdata(pdev);
  143. unsigned flags = mc13xxx_get_flags(priv->mc13xxx);
  144. return flags & MC13XXX_USE_TOUCHSCREEN;
  145. }
  146. static int __init mc13783_adc_probe(struct platform_device *pdev)
  147. {
  148. struct mc13783_adc_priv *priv;
  149. int ret;
  150. const struct platform_device_id *id = platform_get_device_id(pdev);
  151. char *dash;
  152. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  153. if (!priv)
  154. return -ENOMEM;
  155. priv->mc13xxx = dev_get_drvdata(pdev->dev.parent);
  156. snprintf(priv->name, ARRAY_SIZE(priv->name), "%s", id->name);
  157. dash = strchr(priv->name, '-');
  158. if (dash)
  159. *dash = '\0';
  160. platform_set_drvdata(pdev, priv);
  161. /* Register sysfs hooks */
  162. ret = sysfs_create_group(&pdev->dev.kobj, &mc13783_group_base);
  163. if (ret)
  164. return ret;
  165. if (id->driver_data & MC13783_ADC_16CHANS) {
  166. ret = sysfs_create_group(&pdev->dev.kobj,
  167. &mc13783_group_16chans);
  168. if (ret)
  169. goto out_err_create_16chans;
  170. }
  171. if (!mc13783_adc_use_touchscreen(pdev)) {
  172. ret = sysfs_create_group(&pdev->dev.kobj, &mc13783_group_ts);
  173. if (ret)
  174. goto out_err_create_ts;
  175. }
  176. priv->hwmon_dev = hwmon_device_register(&pdev->dev);
  177. if (IS_ERR(priv->hwmon_dev)) {
  178. ret = PTR_ERR(priv->hwmon_dev);
  179. dev_err(&pdev->dev,
  180. "hwmon_device_register failed with %d.\n", ret);
  181. goto out_err_register;
  182. }
  183. return 0;
  184. out_err_register:
  185. if (!mc13783_adc_use_touchscreen(pdev))
  186. sysfs_remove_group(&pdev->dev.kobj, &mc13783_group_ts);
  187. out_err_create_ts:
  188. if (id->driver_data & MC13783_ADC_16CHANS)
  189. sysfs_remove_group(&pdev->dev.kobj, &mc13783_group_16chans);
  190. out_err_create_16chans:
  191. sysfs_remove_group(&pdev->dev.kobj, &mc13783_group_base);
  192. return ret;
  193. }
  194. static int mc13783_adc_remove(struct platform_device *pdev)
  195. {
  196. struct mc13783_adc_priv *priv = platform_get_drvdata(pdev);
  197. kernel_ulong_t driver_data = platform_get_device_id(pdev)->driver_data;
  198. hwmon_device_unregister(priv->hwmon_dev);
  199. if (!mc13783_adc_use_touchscreen(pdev))
  200. sysfs_remove_group(&pdev->dev.kobj, &mc13783_group_ts);
  201. if (driver_data & MC13783_ADC_16CHANS)
  202. sysfs_remove_group(&pdev->dev.kobj, &mc13783_group_16chans);
  203. sysfs_remove_group(&pdev->dev.kobj, &mc13783_group_base);
  204. return 0;
  205. }
  206. static const struct platform_device_id mc13783_adc_idtable[] = {
  207. {
  208. .name = "mc13783-adc",
  209. .driver_data = MC13783_ADC_16CHANS,
  210. }, {
  211. .name = "mc13892-adc",
  212. .driver_data = MC13783_ADC_BPDIV2,
  213. }, {
  214. /* sentinel */
  215. }
  216. };
  217. MODULE_DEVICE_TABLE(platform, mc13783_adc_idtable);
  218. static struct platform_driver mc13783_adc_driver = {
  219. .remove = mc13783_adc_remove,
  220. .driver = {
  221. .name = DRIVER_NAME,
  222. },
  223. .id_table = mc13783_adc_idtable,
  224. };
  225. module_platform_driver_probe(mc13783_adc_driver, mc13783_adc_probe);
  226. MODULE_DESCRIPTION("MC13783 ADC driver");
  227. MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
  228. MODULE_LICENSE("GPL");