vexpress.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License version 2 as
  4. * published by the Free Software Foundation.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * Copyright (C) 2012 ARM Limited
  12. */
  13. #define DRVNAME "vexpress-hwmon"
  14. #define pr_fmt(fmt) DRVNAME ": " fmt
  15. #include <linux/device.h>
  16. #include <linux/err.h>
  17. #include <linux/hwmon.h>
  18. #include <linux/hwmon-sysfs.h>
  19. #include <linux/module.h>
  20. #include <linux/of_device.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/vexpress.h>
  23. struct vexpress_hwmon_data {
  24. struct device *hwmon_dev;
  25. struct vexpress_config_func *func;
  26. };
  27. static ssize_t vexpress_hwmon_name_show(struct device *dev,
  28. struct device_attribute *dev_attr, char *buffer)
  29. {
  30. const char *compatible = of_get_property(dev->of_node, "compatible",
  31. NULL);
  32. return sprintf(buffer, "%s\n", compatible);
  33. }
  34. static ssize_t vexpress_hwmon_label_show(struct device *dev,
  35. struct device_attribute *dev_attr, char *buffer)
  36. {
  37. const char *label = of_get_property(dev->of_node, "label", NULL);
  38. if (!label)
  39. return -ENOENT;
  40. return snprintf(buffer, PAGE_SIZE, "%s\n", label);
  41. }
  42. static ssize_t vexpress_hwmon_u32_show(struct device *dev,
  43. struct device_attribute *dev_attr, char *buffer)
  44. {
  45. struct vexpress_hwmon_data *data = dev_get_drvdata(dev);
  46. int err;
  47. u32 value;
  48. err = vexpress_config_read(data->func, 0, &value);
  49. if (err)
  50. return err;
  51. return snprintf(buffer, PAGE_SIZE, "%u\n", value /
  52. to_sensor_dev_attr(dev_attr)->index);
  53. }
  54. static ssize_t vexpress_hwmon_u64_show(struct device *dev,
  55. struct device_attribute *dev_attr, char *buffer)
  56. {
  57. struct vexpress_hwmon_data *data = dev_get_drvdata(dev);
  58. int err;
  59. u32 value_hi, value_lo;
  60. err = vexpress_config_read(data->func, 0, &value_lo);
  61. if (err)
  62. return err;
  63. err = vexpress_config_read(data->func, 1, &value_hi);
  64. if (err)
  65. return err;
  66. return snprintf(buffer, PAGE_SIZE, "%llu\n",
  67. div_u64(((u64)value_hi << 32) | value_lo,
  68. to_sensor_dev_attr(dev_attr)->index));
  69. }
  70. static DEVICE_ATTR(name, S_IRUGO, vexpress_hwmon_name_show, NULL);
  71. #define VEXPRESS_HWMON_ATTRS(_name, _label_attr, _input_attr) \
  72. struct attribute *vexpress_hwmon_attrs_##_name[] = { \
  73. &dev_attr_name.attr, \
  74. &dev_attr_##_label_attr.attr, \
  75. &sensor_dev_attr_##_input_attr.dev_attr.attr, \
  76. NULL \
  77. }
  78. #if !defined(CONFIG_REGULATOR_VEXPRESS)
  79. static DEVICE_ATTR(in1_label, S_IRUGO, vexpress_hwmon_label_show, NULL);
  80. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, vexpress_hwmon_u32_show,
  81. NULL, 1000);
  82. static VEXPRESS_HWMON_ATTRS(volt, in1_label, in1_input);
  83. static struct attribute_group vexpress_hwmon_group_volt = {
  84. .attrs = vexpress_hwmon_attrs_volt,
  85. };
  86. #endif
  87. static DEVICE_ATTR(curr1_label, S_IRUGO, vexpress_hwmon_label_show, NULL);
  88. static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, vexpress_hwmon_u32_show,
  89. NULL, 1000);
  90. static VEXPRESS_HWMON_ATTRS(amp, curr1_label, curr1_input);
  91. static struct attribute_group vexpress_hwmon_group_amp = {
  92. .attrs = vexpress_hwmon_attrs_amp,
  93. };
  94. static DEVICE_ATTR(temp1_label, S_IRUGO, vexpress_hwmon_label_show, NULL);
  95. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, vexpress_hwmon_u32_show,
  96. NULL, 1000);
  97. static VEXPRESS_HWMON_ATTRS(temp, temp1_label, temp1_input);
  98. static struct attribute_group vexpress_hwmon_group_temp = {
  99. .attrs = vexpress_hwmon_attrs_temp,
  100. };
  101. static DEVICE_ATTR(power1_label, S_IRUGO, vexpress_hwmon_label_show, NULL);
  102. static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, vexpress_hwmon_u32_show,
  103. NULL, 1);
  104. static VEXPRESS_HWMON_ATTRS(power, power1_label, power1_input);
  105. static struct attribute_group vexpress_hwmon_group_power = {
  106. .attrs = vexpress_hwmon_attrs_power,
  107. };
  108. static DEVICE_ATTR(energy1_label, S_IRUGO, vexpress_hwmon_label_show, NULL);
  109. static SENSOR_DEVICE_ATTR(energy1_input, S_IRUGO, vexpress_hwmon_u64_show,
  110. NULL, 1);
  111. static VEXPRESS_HWMON_ATTRS(energy, energy1_label, energy1_input);
  112. static struct attribute_group vexpress_hwmon_group_energy = {
  113. .attrs = vexpress_hwmon_attrs_energy,
  114. };
  115. static struct of_device_id vexpress_hwmon_of_match[] = {
  116. #if !defined(CONFIG_REGULATOR_VEXPRESS)
  117. {
  118. .compatible = "arm,vexpress-volt",
  119. .data = &vexpress_hwmon_group_volt,
  120. },
  121. #endif
  122. {
  123. .compatible = "arm,vexpress-amp",
  124. .data = &vexpress_hwmon_group_amp,
  125. }, {
  126. .compatible = "arm,vexpress-temp",
  127. .data = &vexpress_hwmon_group_temp,
  128. }, {
  129. .compatible = "arm,vexpress-power",
  130. .data = &vexpress_hwmon_group_power,
  131. }, {
  132. .compatible = "arm,vexpress-energy",
  133. .data = &vexpress_hwmon_group_energy,
  134. },
  135. {}
  136. };
  137. MODULE_DEVICE_TABLE(of, vexpress_hwmon_of_match);
  138. static int vexpress_hwmon_probe(struct platform_device *pdev)
  139. {
  140. int err;
  141. const struct of_device_id *match;
  142. struct vexpress_hwmon_data *data;
  143. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  144. if (!data)
  145. return -ENOMEM;
  146. platform_set_drvdata(pdev, data);
  147. match = of_match_device(vexpress_hwmon_of_match, &pdev->dev);
  148. if (!match)
  149. return -ENODEV;
  150. data->func = vexpress_config_func_get_by_dev(&pdev->dev);
  151. if (!data->func)
  152. return -ENODEV;
  153. err = sysfs_create_group(&pdev->dev.kobj, match->data);
  154. if (err)
  155. goto error;
  156. data->hwmon_dev = hwmon_device_register(&pdev->dev);
  157. if (IS_ERR(data->hwmon_dev)) {
  158. err = PTR_ERR(data->hwmon_dev);
  159. goto error;
  160. }
  161. return 0;
  162. error:
  163. sysfs_remove_group(&pdev->dev.kobj, match->data);
  164. vexpress_config_func_put(data->func);
  165. return err;
  166. }
  167. static int vexpress_hwmon_remove(struct platform_device *pdev)
  168. {
  169. struct vexpress_hwmon_data *data = platform_get_drvdata(pdev);
  170. const struct of_device_id *match;
  171. hwmon_device_unregister(data->hwmon_dev);
  172. match = of_match_device(vexpress_hwmon_of_match, &pdev->dev);
  173. sysfs_remove_group(&pdev->dev.kobj, match->data);
  174. vexpress_config_func_put(data->func);
  175. return 0;
  176. }
  177. static struct platform_driver vexpress_hwmon_driver = {
  178. .probe = vexpress_hwmon_probe,
  179. .remove = vexpress_hwmon_remove,
  180. .driver = {
  181. .name = DRVNAME,
  182. .owner = THIS_MODULE,
  183. .of_match_table = vexpress_hwmon_of_match,
  184. },
  185. };
  186. module_platform_driver(vexpress_hwmon_driver);
  187. MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
  188. MODULE_DESCRIPTION("Versatile Express hwmon sensors driver");
  189. MODULE_LICENSE("GPL");
  190. MODULE_ALIAS("platform:vexpress-hwmon");