gsc-hwmon.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for Gateworks System Controller Hardware Monitor module
  4. *
  5. * Copyright (C) 2020 Gateworks Corporation
  6. */
  7. #include <linux/hwmon.h>
  8. #include <linux/hwmon-sysfs.h>
  9. #include <linux/mfd/gsc.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/regmap.h>
  14. #include <linux/slab.h>
  15. #include <linux/platform_data/gsc_hwmon.h>
  16. #define GSC_HWMON_MAX_TEMP_CH 16
  17. #define GSC_HWMON_MAX_IN_CH 16
  18. #define GSC_HWMON_MAX_FAN_CH 16
  19. #define GSC_HWMON_RESOLUTION 12
  20. #define GSC_HWMON_VREF 2500
  21. struct gsc_hwmon_data {
  22. struct gsc_dev *gsc;
  23. struct gsc_hwmon_platform_data *pdata;
  24. struct regmap *regmap;
  25. const struct gsc_hwmon_channel *temp_ch[GSC_HWMON_MAX_TEMP_CH];
  26. const struct gsc_hwmon_channel *in_ch[GSC_HWMON_MAX_IN_CH];
  27. const struct gsc_hwmon_channel *fan_ch[GSC_HWMON_MAX_FAN_CH];
  28. u32 temp_config[GSC_HWMON_MAX_TEMP_CH + 1];
  29. u32 in_config[GSC_HWMON_MAX_IN_CH + 1];
  30. u32 fan_config[GSC_HWMON_MAX_FAN_CH + 1];
  31. struct hwmon_channel_info temp_info;
  32. struct hwmon_channel_info in_info;
  33. struct hwmon_channel_info fan_info;
  34. const struct hwmon_channel_info *info[4];
  35. struct hwmon_chip_info chip;
  36. };
  37. static const struct regmap_bus gsc_hwmon_regmap_bus = {
  38. .reg_read = gsc_read,
  39. .reg_write = gsc_write,
  40. };
  41. static const struct regmap_config gsc_hwmon_regmap_config = {
  42. .reg_bits = 8,
  43. .val_bits = 8,
  44. };
  45. static ssize_t pwm_auto_point_temp_show(struct device *dev,
  46. struct device_attribute *devattr,
  47. char *buf)
  48. {
  49. struct gsc_hwmon_data *hwmon = dev_get_drvdata(dev);
  50. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  51. u8 reg = hwmon->pdata->fan_base + (2 * attr->index);
  52. u8 regs[2];
  53. int ret;
  54. ret = regmap_bulk_read(hwmon->regmap, reg, regs, 2);
  55. if (ret)
  56. return ret;
  57. ret = regs[0] | regs[1] << 8;
  58. return sprintf(buf, "%d\n", ret * 100);
  59. }
  60. static ssize_t pwm_auto_point_temp_store(struct device *dev,
  61. struct device_attribute *devattr,
  62. const char *buf, size_t count)
  63. {
  64. struct gsc_hwmon_data *hwmon = dev_get_drvdata(dev);
  65. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  66. u8 reg = hwmon->pdata->fan_base + (2 * attr->index);
  67. u8 regs[2];
  68. long temp;
  69. int err;
  70. if (kstrtol(buf, 10, &temp))
  71. return -EINVAL;
  72. temp = clamp_val(temp, 0, 100000);
  73. temp = DIV_ROUND_CLOSEST(temp, 100);
  74. regs[0] = temp & 0xff;
  75. regs[1] = (temp >> 8) & 0xff;
  76. err = regmap_bulk_write(hwmon->regmap, reg, regs, 2);
  77. if (err)
  78. return err;
  79. return count;
  80. }
  81. static ssize_t pwm_auto_point_pwm_show(struct device *dev,
  82. struct device_attribute *devattr,
  83. char *buf)
  84. {
  85. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  86. return sprintf(buf, "%d\n", 255 * (50 + (attr->index * 10)) / 100);
  87. }
  88. static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point1_pwm, pwm_auto_point_pwm, 0);
  89. static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_temp, pwm_auto_point_temp, 0);
  90. static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point2_pwm, pwm_auto_point_pwm, 1);
  91. static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point2_temp, pwm_auto_point_temp, 1);
  92. static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point3_pwm, pwm_auto_point_pwm, 2);
  93. static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point3_temp, pwm_auto_point_temp, 2);
  94. static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point4_pwm, pwm_auto_point_pwm, 3);
  95. static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point4_temp, pwm_auto_point_temp, 3);
  96. static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point5_pwm, pwm_auto_point_pwm, 4);
  97. static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point5_temp, pwm_auto_point_temp, 4);
  98. static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point6_pwm, pwm_auto_point_pwm, 5);
  99. static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point6_temp, pwm_auto_point_temp, 5);
  100. static struct attribute *gsc_hwmon_attributes[] = {
  101. &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
  102. &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
  103. &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
  104. &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
  105. &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
  106. &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
  107. &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
  108. &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
  109. &sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr,
  110. &sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr,
  111. &sensor_dev_attr_pwm1_auto_point6_pwm.dev_attr.attr,
  112. &sensor_dev_attr_pwm1_auto_point6_temp.dev_attr.attr,
  113. NULL
  114. };
  115. static const struct attribute_group gsc_hwmon_group = {
  116. .attrs = gsc_hwmon_attributes,
  117. };
  118. __ATTRIBUTE_GROUPS(gsc_hwmon);
  119. static int
  120. gsc_hwmon_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
  121. int channel, long *val)
  122. {
  123. struct gsc_hwmon_data *hwmon = dev_get_drvdata(dev);
  124. const struct gsc_hwmon_channel *ch;
  125. int sz, ret;
  126. long tmp;
  127. u8 buf[3];
  128. switch (type) {
  129. case hwmon_in:
  130. ch = hwmon->in_ch[channel];
  131. break;
  132. case hwmon_temp:
  133. ch = hwmon->temp_ch[channel];
  134. break;
  135. case hwmon_fan:
  136. ch = hwmon->fan_ch[channel];
  137. break;
  138. default:
  139. return -EOPNOTSUPP;
  140. }
  141. sz = (ch->mode == mode_voltage_24bit) ? 3 : 2;
  142. ret = regmap_bulk_read(hwmon->regmap, ch->reg, buf, sz);
  143. if (ret)
  144. return ret;
  145. tmp = 0;
  146. while (sz-- > 0)
  147. tmp |= (buf[sz] << (8 * sz));
  148. switch (ch->mode) {
  149. case mode_temperature:
  150. if (tmp > 0x8000)
  151. tmp -= 0xffff;
  152. tmp *= 100; /* convert to millidegrees celsius */
  153. break;
  154. case mode_voltage_raw:
  155. tmp = clamp_val(tmp, 0, BIT(GSC_HWMON_RESOLUTION));
  156. /* scale based on ref voltage and ADC resolution */
  157. tmp *= GSC_HWMON_VREF;
  158. tmp >>= GSC_HWMON_RESOLUTION;
  159. /* scale based on optional voltage divider */
  160. if (ch->vdiv[0] && ch->vdiv[1]) {
  161. tmp *= (ch->vdiv[0] + ch->vdiv[1]);
  162. tmp /= ch->vdiv[1];
  163. }
  164. /* adjust by uV offset */
  165. tmp += ch->mvoffset;
  166. break;
  167. case mode_fan:
  168. tmp *= 30; /* convert to revolutions per minute */
  169. break;
  170. case mode_voltage_24bit:
  171. case mode_voltage_16bit:
  172. /* no adjustment needed */
  173. break;
  174. }
  175. *val = tmp;
  176. return 0;
  177. }
  178. static int
  179. gsc_hwmon_read_string(struct device *dev, enum hwmon_sensor_types type,
  180. u32 attr, int channel, const char **buf)
  181. {
  182. struct gsc_hwmon_data *hwmon = dev_get_drvdata(dev);
  183. switch (type) {
  184. case hwmon_in:
  185. *buf = hwmon->in_ch[channel]->name;
  186. break;
  187. case hwmon_temp:
  188. *buf = hwmon->temp_ch[channel]->name;
  189. break;
  190. case hwmon_fan:
  191. *buf = hwmon->fan_ch[channel]->name;
  192. break;
  193. default:
  194. return -ENOTSUPP;
  195. }
  196. return 0;
  197. }
  198. static const struct hwmon_ops gsc_hwmon_ops = {
  199. .visible = 0444,
  200. .read = gsc_hwmon_read,
  201. .read_string = gsc_hwmon_read_string,
  202. };
  203. static struct gsc_hwmon_platform_data *
  204. gsc_hwmon_get_devtree_pdata(struct device *dev)
  205. {
  206. struct gsc_hwmon_platform_data *pdata;
  207. struct gsc_hwmon_channel *ch;
  208. struct device_node *fan;
  209. int nchannels;
  210. nchannels = device_get_child_node_count(dev);
  211. if (nchannels == 0)
  212. return ERR_PTR(-ENODEV);
  213. pdata = devm_kzalloc(dev, struct_size(pdata, channels, nchannels),
  214. GFP_KERNEL);
  215. if (!pdata)
  216. return ERR_PTR(-ENOMEM);
  217. pdata->nchannels = nchannels;
  218. /* fan controller base address */
  219. of_node_get(dev->parent->of_node);
  220. fan = of_find_compatible_node(dev->parent->of_node, NULL, "gw,gsc-fan");
  221. if (fan && of_property_read_u32(fan, "reg", &pdata->fan_base)) {
  222. of_node_put(fan);
  223. dev_err(dev, "fan node without base\n");
  224. return ERR_PTR(-EINVAL);
  225. }
  226. of_node_put(fan);
  227. ch = pdata->channels;
  228. /* allocate structures for channels and count instances of each type */
  229. device_for_each_child_node_scoped(dev, child) {
  230. if (fwnode_property_read_string(child, "label", &ch->name)) {
  231. dev_err(dev, "channel without label\n");
  232. return ERR_PTR(-EINVAL);
  233. }
  234. if (fwnode_property_read_u32(child, "reg", &ch->reg)) {
  235. dev_err(dev, "channel without reg\n");
  236. return ERR_PTR(-EINVAL);
  237. }
  238. if (fwnode_property_read_u32(child, "gw,mode", &ch->mode)) {
  239. dev_err(dev, "channel without mode\n");
  240. return ERR_PTR(-EINVAL);
  241. }
  242. if (ch->mode > mode_max) {
  243. dev_err(dev, "invalid channel mode\n");
  244. return ERR_PTR(-EINVAL);
  245. }
  246. if (!fwnode_property_read_u32(child,
  247. "gw,voltage-offset-microvolt",
  248. &ch->mvoffset))
  249. ch->mvoffset /= 1000;
  250. fwnode_property_read_u32_array(child,
  251. "gw,voltage-divider-ohms",
  252. ch->vdiv, ARRAY_SIZE(ch->vdiv));
  253. ch++;
  254. }
  255. return pdata;
  256. }
  257. static int gsc_hwmon_probe(struct platform_device *pdev)
  258. {
  259. struct gsc_dev *gsc = dev_get_drvdata(pdev->dev.parent);
  260. struct device *dev = &pdev->dev;
  261. struct device *hwmon_dev;
  262. struct gsc_hwmon_platform_data *pdata = dev_get_platdata(dev);
  263. struct gsc_hwmon_data *hwmon;
  264. const struct attribute_group **groups;
  265. int i, i_in, i_temp, i_fan;
  266. if (!pdata) {
  267. pdata = gsc_hwmon_get_devtree_pdata(dev);
  268. if (IS_ERR(pdata))
  269. return PTR_ERR(pdata);
  270. }
  271. hwmon = devm_kzalloc(dev, sizeof(*hwmon), GFP_KERNEL);
  272. if (!hwmon)
  273. return -ENOMEM;
  274. hwmon->gsc = gsc;
  275. hwmon->pdata = pdata;
  276. hwmon->regmap = devm_regmap_init(dev, &gsc_hwmon_regmap_bus,
  277. gsc->i2c_hwmon,
  278. &gsc_hwmon_regmap_config);
  279. if (IS_ERR(hwmon->regmap))
  280. return PTR_ERR(hwmon->regmap);
  281. for (i = 0, i_in = 0, i_temp = 0, i_fan = 0; i < hwmon->pdata->nchannels; i++) {
  282. const struct gsc_hwmon_channel *ch = &pdata->channels[i];
  283. switch (ch->mode) {
  284. case mode_temperature:
  285. if (i_temp == GSC_HWMON_MAX_TEMP_CH) {
  286. dev_err(gsc->dev, "too many temp channels\n");
  287. return -EINVAL;
  288. }
  289. hwmon->temp_ch[i_temp] = ch;
  290. hwmon->temp_config[i_temp] = HWMON_T_INPUT |
  291. HWMON_T_LABEL;
  292. i_temp++;
  293. break;
  294. case mode_fan:
  295. if (i_fan == GSC_HWMON_MAX_FAN_CH) {
  296. dev_err(gsc->dev, "too many fan channels\n");
  297. return -EINVAL;
  298. }
  299. hwmon->fan_ch[i_fan] = ch;
  300. hwmon->fan_config[i_fan] = HWMON_F_INPUT |
  301. HWMON_F_LABEL;
  302. i_fan++;
  303. break;
  304. case mode_voltage_24bit:
  305. case mode_voltage_16bit:
  306. case mode_voltage_raw:
  307. if (i_in == GSC_HWMON_MAX_IN_CH) {
  308. dev_err(gsc->dev, "too many input channels\n");
  309. return -EINVAL;
  310. }
  311. hwmon->in_ch[i_in] = ch;
  312. hwmon->in_config[i_in] =
  313. HWMON_I_INPUT | HWMON_I_LABEL;
  314. i_in++;
  315. break;
  316. default:
  317. dev_err(gsc->dev, "invalid mode: %d\n", ch->mode);
  318. return -EINVAL;
  319. }
  320. }
  321. /* setup config structures */
  322. hwmon->chip.ops = &gsc_hwmon_ops;
  323. hwmon->chip.info = hwmon->info;
  324. hwmon->info[0] = &hwmon->temp_info;
  325. hwmon->info[1] = &hwmon->in_info;
  326. hwmon->info[2] = &hwmon->fan_info;
  327. hwmon->temp_info.type = hwmon_temp;
  328. hwmon->temp_info.config = hwmon->temp_config;
  329. hwmon->in_info.type = hwmon_in;
  330. hwmon->in_info.config = hwmon->in_config;
  331. hwmon->fan_info.type = hwmon_fan;
  332. hwmon->fan_info.config = hwmon->fan_config;
  333. groups = pdata->fan_base ? gsc_hwmon_groups : NULL;
  334. hwmon_dev = devm_hwmon_device_register_with_info(dev,
  335. KBUILD_MODNAME, hwmon,
  336. &hwmon->chip, groups);
  337. return PTR_ERR_OR_ZERO(hwmon_dev);
  338. }
  339. static const struct of_device_id gsc_hwmon_of_match[] = {
  340. { .compatible = "gw,gsc-adc", },
  341. {}
  342. };
  343. MODULE_DEVICE_TABLE(of, gsc_hwmon_of_match);
  344. static struct platform_driver gsc_hwmon_driver = {
  345. .driver = {
  346. .name = "gsc-hwmon",
  347. .of_match_table = gsc_hwmon_of_match,
  348. },
  349. .probe = gsc_hwmon_probe,
  350. };
  351. module_platform_driver(gsc_hwmon_driver);
  352. MODULE_AUTHOR("Tim Harvey <tharvey@gateworks.com>");
  353. MODULE_DESCRIPTION("GSC hardware monitor driver");
  354. MODULE_LICENSE("GPL v2");