gsc-hwmon.c 11 KB

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