vexpress.c 6.3 KB

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