twl4030-madc-hwmon.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. *
  3. * TWL4030 MADC Hwmon driver-This driver monitors the real time
  4. * conversion of analog signals like battery temperature,
  5. * battery type, battery level etc. User can ask for the conversion on a
  6. * particular channel using the sysfs nodes.
  7. *
  8. * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
  9. * J Keerthy <j-keerthy@ti.com>
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * version 2 as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  23. * 02110-1301 USA
  24. *
  25. */
  26. #include <linux/init.h>
  27. #include <linux/module.h>
  28. #include <linux/kernel.h>
  29. #include <linux/i2c/twl.h>
  30. #include <linux/device.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/i2c/twl4030-madc.h>
  33. #include <linux/hwmon.h>
  34. #include <linux/hwmon-sysfs.h>
  35. #include <linux/stddef.h>
  36. #include <linux/sysfs.h>
  37. #include <linux/err.h>
  38. #include <linux/types.h>
  39. /*
  40. * sysfs hook function
  41. */
  42. static ssize_t madc_read(struct device *dev,
  43. struct device_attribute *devattr, char *buf)
  44. {
  45. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  46. struct twl4030_madc_request req;
  47. long val;
  48. req.channels = (1 << attr->index);
  49. req.method = TWL4030_MADC_SW2;
  50. req.func_cb = NULL;
  51. val = twl4030_madc_conversion(&req);
  52. if (val < 0)
  53. return val;
  54. return sprintf(buf, "%d\n", req.rbuf[attr->index]);
  55. }
  56. /* sysfs nodes to read individual channels from user side */
  57. static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, madc_read, NULL, 0);
  58. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, madc_read, NULL, 1);
  59. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, madc_read, NULL, 2);
  60. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, madc_read, NULL, 3);
  61. static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, madc_read, NULL, 4);
  62. static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, madc_read, NULL, 5);
  63. static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, madc_read, NULL, 6);
  64. static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, madc_read, NULL, 7);
  65. static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, madc_read, NULL, 8);
  66. static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, madc_read, NULL, 9);
  67. static SENSOR_DEVICE_ATTR(curr10_input, S_IRUGO, madc_read, NULL, 10);
  68. static SENSOR_DEVICE_ATTR(in11_input, S_IRUGO, madc_read, NULL, 11);
  69. static SENSOR_DEVICE_ATTR(in12_input, S_IRUGO, madc_read, NULL, 12);
  70. static SENSOR_DEVICE_ATTR(in15_input, S_IRUGO, madc_read, NULL, 15);
  71. static struct attribute *twl4030_madc_attributes[] = {
  72. &sensor_dev_attr_in0_input.dev_attr.attr,
  73. &sensor_dev_attr_temp1_input.dev_attr.attr,
  74. &sensor_dev_attr_in2_input.dev_attr.attr,
  75. &sensor_dev_attr_in3_input.dev_attr.attr,
  76. &sensor_dev_attr_in4_input.dev_attr.attr,
  77. &sensor_dev_attr_in5_input.dev_attr.attr,
  78. &sensor_dev_attr_in6_input.dev_attr.attr,
  79. &sensor_dev_attr_in7_input.dev_attr.attr,
  80. &sensor_dev_attr_in8_input.dev_attr.attr,
  81. &sensor_dev_attr_in9_input.dev_attr.attr,
  82. &sensor_dev_attr_curr10_input.dev_attr.attr,
  83. &sensor_dev_attr_in11_input.dev_attr.attr,
  84. &sensor_dev_attr_in12_input.dev_attr.attr,
  85. &sensor_dev_attr_in15_input.dev_attr.attr,
  86. NULL
  87. };
  88. static const struct attribute_group twl4030_madc_group = {
  89. .attrs = twl4030_madc_attributes,
  90. };
  91. static int __devinit twl4030_madc_hwmon_probe(struct platform_device *pdev)
  92. {
  93. int ret;
  94. struct device *hwmon;
  95. ret = sysfs_create_group(&pdev->dev.kobj, &twl4030_madc_group);
  96. if (ret)
  97. goto err_sysfs;
  98. hwmon = hwmon_device_register(&pdev->dev);
  99. if (IS_ERR(hwmon)) {
  100. dev_err(&pdev->dev, "hwmon_device_register failed.\n");
  101. ret = PTR_ERR(hwmon);
  102. goto err_reg;
  103. }
  104. return 0;
  105. err_reg:
  106. sysfs_remove_group(&pdev->dev.kobj, &twl4030_madc_group);
  107. err_sysfs:
  108. return ret;
  109. }
  110. static int __devexit twl4030_madc_hwmon_remove(struct platform_device *pdev)
  111. {
  112. hwmon_device_unregister(&pdev->dev);
  113. sysfs_remove_group(&pdev->dev.kobj, &twl4030_madc_group);
  114. return 0;
  115. }
  116. static struct platform_driver twl4030_madc_hwmon_driver = {
  117. .probe = twl4030_madc_hwmon_probe,
  118. .remove = __exit_p(twl4030_madc_hwmon_remove),
  119. .driver = {
  120. .name = "twl4030_madc_hwmon",
  121. .owner = THIS_MODULE,
  122. },
  123. };
  124. static int __init twl4030_madc_hwmon_init(void)
  125. {
  126. return platform_driver_register(&twl4030_madc_hwmon_driver);
  127. }
  128. module_init(twl4030_madc_hwmon_init);
  129. static void __exit twl4030_madc_hwmon_exit(void)
  130. {
  131. platform_driver_unregister(&twl4030_madc_hwmon_driver);
  132. }
  133. module_exit(twl4030_madc_hwmon_exit);
  134. MODULE_DESCRIPTION("TWL4030 ADC Hwmon driver");
  135. MODULE_LICENSE("GPL");
  136. MODULE_AUTHOR("J Keerthy");
  137. MODULE_ALIAS("platform:twl4030_madc_hwmon");