vexpress.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 regmap *reg;
  27. };
  28. static ssize_t vexpress_hwmon_label_show(struct device *dev,
  29. struct device_attribute *dev_attr, char *buffer)
  30. {
  31. const char *label = of_get_property(dev->of_node, "label", NULL);
  32. return snprintf(buffer, PAGE_SIZE, "%s\n", label);
  33. }
  34. static ssize_t vexpress_hwmon_u32_show(struct device *dev,
  35. struct device_attribute *dev_attr, char *buffer)
  36. {
  37. struct vexpress_hwmon_data *data = dev_get_drvdata(dev);
  38. int err;
  39. u32 value;
  40. err = regmap_read(data->reg, 0, &value);
  41. if (err)
  42. return err;
  43. return snprintf(buffer, PAGE_SIZE, "%u\n", value /
  44. to_sensor_dev_attr(dev_attr)->index);
  45. }
  46. static ssize_t vexpress_hwmon_u64_show(struct device *dev,
  47. struct device_attribute *dev_attr, char *buffer)
  48. {
  49. struct vexpress_hwmon_data *data = dev_get_drvdata(dev);
  50. int err;
  51. u32 value_hi, value_lo;
  52. err = regmap_read(data->reg, 0, &value_lo);
  53. if (err)
  54. return err;
  55. err = regmap_read(data->reg, 1, &value_hi);
  56. if (err)
  57. return err;
  58. return snprintf(buffer, PAGE_SIZE, "%llu\n",
  59. div_u64(((u64)value_hi << 32) | value_lo,
  60. to_sensor_dev_attr(dev_attr)->index));
  61. }
  62. static umode_t vexpress_hwmon_attr_is_visible(struct kobject *kobj,
  63. struct attribute *attr, int index)
  64. {
  65. struct device *dev = kobj_to_dev(kobj);
  66. struct device_attribute *dev_attr = container_of(attr,
  67. struct device_attribute, attr);
  68. if (dev_attr->show == vexpress_hwmon_label_show &&
  69. !of_get_property(dev->of_node, "label", NULL))
  70. return 0;
  71. return attr->mode;
  72. }
  73. struct vexpress_hwmon_type {
  74. const char *name;
  75. const struct attribute_group **attr_groups;
  76. };
  77. #if !defined(CONFIG_REGULATOR_VEXPRESS)
  78. static DEVICE_ATTR(in1_label, S_IRUGO, vexpress_hwmon_label_show, NULL);
  79. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, vexpress_hwmon_u32_show,
  80. NULL, 1000);
  81. static struct attribute *vexpress_hwmon_attrs_volt[] = {
  82. &dev_attr_in1_label.attr,
  83. &sensor_dev_attr_in1_input.dev_attr.attr,
  84. NULL
  85. };
  86. static struct attribute_group vexpress_hwmon_group_volt = {
  87. .is_visible = vexpress_hwmon_attr_is_visible,
  88. .attrs = vexpress_hwmon_attrs_volt,
  89. };
  90. static struct vexpress_hwmon_type vexpress_hwmon_volt = {
  91. .name = "vexpress_volt",
  92. .attr_groups = (const struct attribute_group *[]) {
  93. &vexpress_hwmon_group_volt,
  94. NULL,
  95. },
  96. };
  97. #endif
  98. static DEVICE_ATTR(curr1_label, S_IRUGO, vexpress_hwmon_label_show, NULL);
  99. static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, vexpress_hwmon_u32_show,
  100. NULL, 1000);
  101. static struct attribute *vexpress_hwmon_attrs_amp[] = {
  102. &dev_attr_curr1_label.attr,
  103. &sensor_dev_attr_curr1_input.dev_attr.attr,
  104. NULL
  105. };
  106. static struct attribute_group vexpress_hwmon_group_amp = {
  107. .is_visible = vexpress_hwmon_attr_is_visible,
  108. .attrs = vexpress_hwmon_attrs_amp,
  109. };
  110. static struct vexpress_hwmon_type vexpress_hwmon_amp = {
  111. .name = "vexpress_amp",
  112. .attr_groups = (const struct attribute_group *[]) {
  113. &vexpress_hwmon_group_amp,
  114. NULL
  115. },
  116. };
  117. static DEVICE_ATTR(temp1_label, S_IRUGO, vexpress_hwmon_label_show, NULL);
  118. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, vexpress_hwmon_u32_show,
  119. NULL, 1000);
  120. static struct attribute *vexpress_hwmon_attrs_temp[] = {
  121. &dev_attr_temp1_label.attr,
  122. &sensor_dev_attr_temp1_input.dev_attr.attr,
  123. NULL
  124. };
  125. static struct attribute_group vexpress_hwmon_group_temp = {
  126. .is_visible = vexpress_hwmon_attr_is_visible,
  127. .attrs = vexpress_hwmon_attrs_temp,
  128. };
  129. static struct vexpress_hwmon_type vexpress_hwmon_temp = {
  130. .name = "vexpress_temp",
  131. .attr_groups = (const struct attribute_group *[]) {
  132. &vexpress_hwmon_group_temp,
  133. NULL
  134. },
  135. };
  136. static DEVICE_ATTR(power1_label, S_IRUGO, vexpress_hwmon_label_show, NULL);
  137. static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, vexpress_hwmon_u32_show,
  138. NULL, 1);
  139. static struct attribute *vexpress_hwmon_attrs_power[] = {
  140. &dev_attr_power1_label.attr,
  141. &sensor_dev_attr_power1_input.dev_attr.attr,
  142. NULL
  143. };
  144. static struct attribute_group vexpress_hwmon_group_power = {
  145. .is_visible = vexpress_hwmon_attr_is_visible,
  146. .attrs = vexpress_hwmon_attrs_power,
  147. };
  148. static struct vexpress_hwmon_type vexpress_hwmon_power = {
  149. .name = "vexpress_power",
  150. .attr_groups = (const struct attribute_group *[]) {
  151. &vexpress_hwmon_group_power,
  152. NULL
  153. },
  154. };
  155. static DEVICE_ATTR(energy1_label, S_IRUGO, vexpress_hwmon_label_show, NULL);
  156. static SENSOR_DEVICE_ATTR(energy1_input, S_IRUGO, vexpress_hwmon_u64_show,
  157. NULL, 1);
  158. static struct attribute *vexpress_hwmon_attrs_energy[] = {
  159. &dev_attr_energy1_label.attr,
  160. &sensor_dev_attr_energy1_input.dev_attr.attr,
  161. NULL
  162. };
  163. static struct attribute_group vexpress_hwmon_group_energy = {
  164. .is_visible = vexpress_hwmon_attr_is_visible,
  165. .attrs = vexpress_hwmon_attrs_energy,
  166. };
  167. static struct vexpress_hwmon_type vexpress_hwmon_energy = {
  168. .name = "vexpress_energy",
  169. .attr_groups = (const struct attribute_group *[]) {
  170. &vexpress_hwmon_group_energy,
  171. NULL
  172. },
  173. };
  174. static const struct of_device_id vexpress_hwmon_of_match[] = {
  175. #if !defined(CONFIG_REGULATOR_VEXPRESS)
  176. {
  177. .compatible = "arm,vexpress-volt",
  178. .data = &vexpress_hwmon_volt,
  179. },
  180. #endif
  181. {
  182. .compatible = "arm,vexpress-amp",
  183. .data = &vexpress_hwmon_amp,
  184. }, {
  185. .compatible = "arm,vexpress-temp",
  186. .data = &vexpress_hwmon_temp,
  187. }, {
  188. .compatible = "arm,vexpress-power",
  189. .data = &vexpress_hwmon_power,
  190. }, {
  191. .compatible = "arm,vexpress-energy",
  192. .data = &vexpress_hwmon_energy,
  193. },
  194. {}
  195. };
  196. MODULE_DEVICE_TABLE(of, vexpress_hwmon_of_match);
  197. static int vexpress_hwmon_probe(struct platform_device *pdev)
  198. {
  199. const struct of_device_id *match;
  200. struct vexpress_hwmon_data *data;
  201. const struct vexpress_hwmon_type *type;
  202. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  203. if (!data)
  204. return -ENOMEM;
  205. platform_set_drvdata(pdev, data);
  206. match = of_match_device(vexpress_hwmon_of_match, &pdev->dev);
  207. if (!match)
  208. return -ENODEV;
  209. type = match->data;
  210. data->reg = devm_regmap_init_vexpress_config(&pdev->dev);
  211. if (IS_ERR(data->reg))
  212. return PTR_ERR(data->reg);
  213. data->hwmon_dev = devm_hwmon_device_register_with_groups(&pdev->dev,
  214. type->name, data, type->attr_groups);
  215. return PTR_ERR_OR_ZERO(data->hwmon_dev);
  216. }
  217. static struct platform_driver vexpress_hwmon_driver = {
  218. .probe = vexpress_hwmon_probe,
  219. .driver = {
  220. .name = DRVNAME,
  221. .of_match_table = vexpress_hwmon_of_match,
  222. },
  223. };
  224. module_platform_driver(vexpress_hwmon_driver);
  225. MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
  226. MODULE_DESCRIPTION("Versatile Express hwmon sensors driver");
  227. MODULE_LICENSE("GPL");
  228. MODULE_ALIAS("platform:vexpress-hwmon");