emc1403.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * emc1403.c - SMSC Thermal Driver
  4. *
  5. * Copyright (C) 2008 Intel Corp
  6. *
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. *
  9. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/slab.h>
  14. #include <linux/i2c.h>
  15. #include <linux/hwmon.h>
  16. #include <linux/hwmon-sysfs.h>
  17. #include <linux/err.h>
  18. #include <linux/sysfs.h>
  19. #include <linux/mutex.h>
  20. #include <linux/regmap.h>
  21. #define THERMAL_PID_REG 0xfd
  22. #define THERMAL_SMSC_ID_REG 0xfe
  23. #define THERMAL_REVISION_REG 0xff
  24. enum emc1403_chip { emc1402, emc1403, emc1404 };
  25. struct thermal_data {
  26. struct regmap *regmap;
  27. struct mutex mutex;
  28. const struct attribute_group *groups[4];
  29. };
  30. static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
  31. char *buf)
  32. {
  33. struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
  34. struct thermal_data *data = dev_get_drvdata(dev);
  35. unsigned int val;
  36. int retval;
  37. retval = regmap_read(data->regmap, sda->index, &val);
  38. if (retval < 0)
  39. return retval;
  40. return sprintf(buf, "%d000\n", val);
  41. }
  42. static ssize_t bit_show(struct device *dev, struct device_attribute *attr,
  43. char *buf)
  44. {
  45. struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr);
  46. struct thermal_data *data = dev_get_drvdata(dev);
  47. unsigned int val;
  48. int retval;
  49. retval = regmap_read(data->regmap, sda->nr, &val);
  50. if (retval < 0)
  51. return retval;
  52. return sprintf(buf, "%d\n", !!(val & sda->index));
  53. }
  54. static ssize_t temp_store(struct device *dev, struct device_attribute *attr,
  55. const char *buf, size_t count)
  56. {
  57. struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
  58. struct thermal_data *data = dev_get_drvdata(dev);
  59. unsigned long val;
  60. int retval;
  61. if (kstrtoul(buf, 10, &val))
  62. return -EINVAL;
  63. retval = regmap_write(data->regmap, sda->index,
  64. DIV_ROUND_CLOSEST(val, 1000));
  65. if (retval < 0)
  66. return retval;
  67. return count;
  68. }
  69. static ssize_t bit_store(struct device *dev, struct device_attribute *attr,
  70. const char *buf, size_t count)
  71. {
  72. struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr);
  73. struct thermal_data *data = dev_get_drvdata(dev);
  74. unsigned long val;
  75. int retval;
  76. if (kstrtoul(buf, 10, &val))
  77. return -EINVAL;
  78. retval = regmap_update_bits(data->regmap, sda->nr, sda->index,
  79. val ? sda->index : 0);
  80. if (retval < 0)
  81. return retval;
  82. return count;
  83. }
  84. static ssize_t show_hyst_common(struct device *dev,
  85. struct device_attribute *attr, char *buf,
  86. bool is_min)
  87. {
  88. struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
  89. struct thermal_data *data = dev_get_drvdata(dev);
  90. struct regmap *regmap = data->regmap;
  91. unsigned int limit;
  92. unsigned int hyst;
  93. int retval;
  94. retval = regmap_read(regmap, sda->index, &limit);
  95. if (retval < 0)
  96. return retval;
  97. retval = regmap_read(regmap, 0x21, &hyst);
  98. if (retval < 0)
  99. return retval;
  100. return sprintf(buf, "%d000\n", is_min ? limit + hyst : limit - hyst);
  101. }
  102. static ssize_t hyst_show(struct device *dev, struct device_attribute *attr,
  103. char *buf)
  104. {
  105. return show_hyst_common(dev, attr, buf, false);
  106. }
  107. static ssize_t min_hyst_show(struct device *dev,
  108. struct device_attribute *attr, char *buf)
  109. {
  110. return show_hyst_common(dev, attr, buf, true);
  111. }
  112. static ssize_t hyst_store(struct device *dev, struct device_attribute *attr,
  113. const char *buf, size_t count)
  114. {
  115. struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
  116. struct thermal_data *data = dev_get_drvdata(dev);
  117. struct regmap *regmap = data->regmap;
  118. unsigned int limit;
  119. int retval;
  120. int hyst;
  121. unsigned long val;
  122. if (kstrtoul(buf, 10, &val))
  123. return -EINVAL;
  124. mutex_lock(&data->mutex);
  125. retval = regmap_read(regmap, sda->index, &limit);
  126. if (retval < 0)
  127. goto fail;
  128. hyst = limit * 1000 - val;
  129. hyst = clamp_val(DIV_ROUND_CLOSEST(hyst, 1000), 0, 255);
  130. retval = regmap_write(regmap, 0x21, hyst);
  131. if (retval == 0)
  132. retval = count;
  133. fail:
  134. mutex_unlock(&data->mutex);
  135. return retval;
  136. }
  137. /*
  138. * Sensors. We pass the actual i2c register to the methods.
  139. */
  140. static SENSOR_DEVICE_ATTR_RW(temp1_min, temp, 0x06);
  141. static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, 0x05);
  142. static SENSOR_DEVICE_ATTR_RW(temp1_crit, temp, 0x20);
  143. static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0x00);
  144. static SENSOR_DEVICE_ATTR_2_RO(temp1_min_alarm, bit, 0x36, 0x01);
  145. static SENSOR_DEVICE_ATTR_2_RO(temp1_max_alarm, bit, 0x35, 0x01);
  146. static SENSOR_DEVICE_ATTR_2_RO(temp1_crit_alarm, bit, 0x37, 0x01);
  147. static SENSOR_DEVICE_ATTR_RO(temp1_min_hyst, min_hyst, 0x06);
  148. static SENSOR_DEVICE_ATTR_RO(temp1_max_hyst, hyst, 0x05);
  149. static SENSOR_DEVICE_ATTR_RW(temp1_crit_hyst, hyst, 0x20);
  150. static SENSOR_DEVICE_ATTR_RW(temp2_min, temp, 0x08);
  151. static SENSOR_DEVICE_ATTR_RW(temp2_max, temp, 0x07);
  152. static SENSOR_DEVICE_ATTR_RW(temp2_crit, temp, 0x19);
  153. static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 0x01);
  154. static SENSOR_DEVICE_ATTR_2_RO(temp2_fault, bit, 0x1b, 0x02);
  155. static SENSOR_DEVICE_ATTR_2_RO(temp2_min_alarm, bit, 0x36, 0x02);
  156. static SENSOR_DEVICE_ATTR_2_RO(temp2_max_alarm, bit, 0x35, 0x02);
  157. static SENSOR_DEVICE_ATTR_2_RO(temp2_crit_alarm, bit, 0x37, 0x02);
  158. static SENSOR_DEVICE_ATTR_RO(temp2_min_hyst, min_hyst, 0x08);
  159. static SENSOR_DEVICE_ATTR_RO(temp2_max_hyst, hyst, 0x07);
  160. static SENSOR_DEVICE_ATTR_RO(temp2_crit_hyst, hyst, 0x19);
  161. static SENSOR_DEVICE_ATTR_RW(temp3_min, temp, 0x16);
  162. static SENSOR_DEVICE_ATTR_RW(temp3_max, temp, 0x15);
  163. static SENSOR_DEVICE_ATTR_RW(temp3_crit, temp, 0x1A);
  164. static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 0x23);
  165. static SENSOR_DEVICE_ATTR_2_RO(temp3_fault, bit, 0x1b, 0x04);
  166. static SENSOR_DEVICE_ATTR_2_RO(temp3_min_alarm, bit, 0x36, 0x04);
  167. static SENSOR_DEVICE_ATTR_2_RO(temp3_max_alarm, bit, 0x35, 0x04);
  168. static SENSOR_DEVICE_ATTR_2_RO(temp3_crit_alarm, bit, 0x37, 0x04);
  169. static SENSOR_DEVICE_ATTR_RO(temp3_min_hyst, min_hyst, 0x16);
  170. static SENSOR_DEVICE_ATTR_RO(temp3_max_hyst, hyst, 0x15);
  171. static SENSOR_DEVICE_ATTR_RO(temp3_crit_hyst, hyst, 0x1A);
  172. static SENSOR_DEVICE_ATTR_RW(temp4_min, temp, 0x2D);
  173. static SENSOR_DEVICE_ATTR_RW(temp4_max, temp, 0x2C);
  174. static SENSOR_DEVICE_ATTR_RW(temp4_crit, temp, 0x30);
  175. static SENSOR_DEVICE_ATTR_RO(temp4_input, temp, 0x2A);
  176. static SENSOR_DEVICE_ATTR_2_RO(temp4_fault, bit, 0x1b, 0x08);
  177. static SENSOR_DEVICE_ATTR_2_RO(temp4_min_alarm, bit, 0x36, 0x08);
  178. static SENSOR_DEVICE_ATTR_2_RO(temp4_max_alarm, bit, 0x35, 0x08);
  179. static SENSOR_DEVICE_ATTR_2_RO(temp4_crit_alarm, bit, 0x37, 0x08);
  180. static SENSOR_DEVICE_ATTR_RO(temp4_min_hyst, min_hyst, 0x2D);
  181. static SENSOR_DEVICE_ATTR_RO(temp4_max_hyst, hyst, 0x2C);
  182. static SENSOR_DEVICE_ATTR_RO(temp4_crit_hyst, hyst, 0x30);
  183. static SENSOR_DEVICE_ATTR_2_RW(power_state, bit, 0x03, 0x40);
  184. static struct attribute *emc1402_attrs[] = {
  185. &sensor_dev_attr_temp1_min.dev_attr.attr,
  186. &sensor_dev_attr_temp1_max.dev_attr.attr,
  187. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  188. &sensor_dev_attr_temp1_input.dev_attr.attr,
  189. &sensor_dev_attr_temp1_min_hyst.dev_attr.attr,
  190. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  191. &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
  192. &sensor_dev_attr_temp2_min.dev_attr.attr,
  193. &sensor_dev_attr_temp2_max.dev_attr.attr,
  194. &sensor_dev_attr_temp2_crit.dev_attr.attr,
  195. &sensor_dev_attr_temp2_input.dev_attr.attr,
  196. &sensor_dev_attr_temp2_min_hyst.dev_attr.attr,
  197. &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
  198. &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
  199. &sensor_dev_attr_power_state.dev_attr.attr,
  200. NULL
  201. };
  202. static const struct attribute_group emc1402_group = {
  203. .attrs = emc1402_attrs,
  204. };
  205. static struct attribute *emc1403_attrs[] = {
  206. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  207. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  208. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  209. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  210. &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
  211. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  212. &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
  213. &sensor_dev_attr_temp3_min.dev_attr.attr,
  214. &sensor_dev_attr_temp3_max.dev_attr.attr,
  215. &sensor_dev_attr_temp3_crit.dev_attr.attr,
  216. &sensor_dev_attr_temp3_input.dev_attr.attr,
  217. &sensor_dev_attr_temp3_fault.dev_attr.attr,
  218. &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
  219. &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
  220. &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
  221. &sensor_dev_attr_temp3_min_hyst.dev_attr.attr,
  222. &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
  223. &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
  224. NULL
  225. };
  226. static const struct attribute_group emc1403_group = {
  227. .attrs = emc1403_attrs,
  228. };
  229. static struct attribute *emc1404_attrs[] = {
  230. &sensor_dev_attr_temp4_min.dev_attr.attr,
  231. &sensor_dev_attr_temp4_max.dev_attr.attr,
  232. &sensor_dev_attr_temp4_crit.dev_attr.attr,
  233. &sensor_dev_attr_temp4_input.dev_attr.attr,
  234. &sensor_dev_attr_temp4_fault.dev_attr.attr,
  235. &sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
  236. &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
  237. &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr,
  238. &sensor_dev_attr_temp4_min_hyst.dev_attr.attr,
  239. &sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
  240. &sensor_dev_attr_temp4_crit_hyst.dev_attr.attr,
  241. NULL
  242. };
  243. static const struct attribute_group emc1404_group = {
  244. .attrs = emc1404_attrs,
  245. };
  246. /*
  247. * EMC14x2 uses a different register and different bits to report alarm and
  248. * fault status. For simplicity, provide a separate attribute group for this
  249. * chip series.
  250. * Since we can not re-use the same attribute names, create a separate attribute
  251. * array.
  252. */
  253. static struct sensor_device_attribute_2 emc1402_alarms[] = {
  254. SENSOR_ATTR_2_RO(temp1_min_alarm, bit, 0x02, 0x20),
  255. SENSOR_ATTR_2_RO(temp1_max_alarm, bit, 0x02, 0x40),
  256. SENSOR_ATTR_2_RO(temp1_crit_alarm, bit, 0x02, 0x01),
  257. SENSOR_ATTR_2_RO(temp2_fault, bit, 0x02, 0x04),
  258. SENSOR_ATTR_2_RO(temp2_min_alarm, bit, 0x02, 0x08),
  259. SENSOR_ATTR_2_RO(temp2_max_alarm, bit, 0x02, 0x10),
  260. SENSOR_ATTR_2_RO(temp2_crit_alarm, bit, 0x02, 0x02),
  261. };
  262. static struct attribute *emc1402_alarm_attrs[] = {
  263. &emc1402_alarms[0].dev_attr.attr,
  264. &emc1402_alarms[1].dev_attr.attr,
  265. &emc1402_alarms[2].dev_attr.attr,
  266. &emc1402_alarms[3].dev_attr.attr,
  267. &emc1402_alarms[4].dev_attr.attr,
  268. &emc1402_alarms[5].dev_attr.attr,
  269. &emc1402_alarms[6].dev_attr.attr,
  270. NULL,
  271. };
  272. static const struct attribute_group emc1402_alarm_group = {
  273. .attrs = emc1402_alarm_attrs,
  274. };
  275. static int emc1403_detect(struct i2c_client *client,
  276. struct i2c_board_info *info)
  277. {
  278. int id;
  279. /* Check if thermal chip is SMSC and EMC1403 or EMC1423 */
  280. id = i2c_smbus_read_byte_data(client, THERMAL_SMSC_ID_REG);
  281. if (id != 0x5d)
  282. return -ENODEV;
  283. id = i2c_smbus_read_byte_data(client, THERMAL_PID_REG);
  284. switch (id) {
  285. case 0x20:
  286. strscpy(info->type, "emc1402", I2C_NAME_SIZE);
  287. break;
  288. case 0x21:
  289. strscpy(info->type, "emc1403", I2C_NAME_SIZE);
  290. break;
  291. case 0x22:
  292. strscpy(info->type, "emc1422", I2C_NAME_SIZE);
  293. break;
  294. case 0x23:
  295. strscpy(info->type, "emc1423", I2C_NAME_SIZE);
  296. break;
  297. case 0x25:
  298. strscpy(info->type, "emc1404", I2C_NAME_SIZE);
  299. break;
  300. case 0x27:
  301. strscpy(info->type, "emc1424", I2C_NAME_SIZE);
  302. break;
  303. default:
  304. return -ENODEV;
  305. }
  306. id = i2c_smbus_read_byte_data(client, THERMAL_REVISION_REG);
  307. if (id < 0x01 || id > 0x04)
  308. return -ENODEV;
  309. return 0;
  310. }
  311. static bool emc1403_regmap_is_volatile(struct device *dev, unsigned int reg)
  312. {
  313. switch (reg) {
  314. case 0x00: /* internal diode high byte */
  315. case 0x01: /* external diode 1 high byte */
  316. case 0x02: /* status */
  317. case 0x10: /* external diode 1 low byte */
  318. case 0x1b: /* external diode fault */
  319. case 0x23: /* external diode 2 high byte */
  320. case 0x24: /* external diode 2 low byte */
  321. case 0x29: /* internal diode low byte */
  322. case 0x2a: /* externl diode 3 high byte */
  323. case 0x2b: /* external diode 3 low byte */
  324. case 0x35: /* high limit status */
  325. case 0x36: /* low limit status */
  326. case 0x37: /* therm limit status */
  327. return true;
  328. default:
  329. return false;
  330. }
  331. }
  332. static const struct regmap_config emc1403_regmap_config = {
  333. .reg_bits = 8,
  334. .val_bits = 8,
  335. .cache_type = REGCACHE_RBTREE,
  336. .volatile_reg = emc1403_regmap_is_volatile,
  337. };
  338. static const struct i2c_device_id emc1403_idtable[];
  339. static int emc1403_probe(struct i2c_client *client)
  340. {
  341. struct thermal_data *data;
  342. struct device *hwmon_dev;
  343. const struct i2c_device_id *id = i2c_match_id(emc1403_idtable, client);
  344. data = devm_kzalloc(&client->dev, sizeof(struct thermal_data),
  345. GFP_KERNEL);
  346. if (data == NULL)
  347. return -ENOMEM;
  348. data->regmap = devm_regmap_init_i2c(client, &emc1403_regmap_config);
  349. if (IS_ERR(data->regmap))
  350. return PTR_ERR(data->regmap);
  351. mutex_init(&data->mutex);
  352. switch (id->driver_data) {
  353. case emc1404:
  354. data->groups[2] = &emc1404_group;
  355. fallthrough;
  356. case emc1403:
  357. data->groups[1] = &emc1403_group;
  358. fallthrough;
  359. case emc1402:
  360. data->groups[0] = &emc1402_group;
  361. }
  362. if (id->driver_data == emc1402)
  363. data->groups[1] = &emc1402_alarm_group;
  364. hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
  365. client->name, data,
  366. data->groups);
  367. if (IS_ERR(hwmon_dev))
  368. return PTR_ERR(hwmon_dev);
  369. dev_info(&client->dev, "%s Thermal chip found\n", id->name);
  370. return 0;
  371. }
  372. static const unsigned short emc1403_address_list[] = {
  373. 0x18, 0x1c, 0x29, 0x4c, 0x4d, 0x5c, I2C_CLIENT_END
  374. };
  375. /* Last digit of chip name indicates number of channels */
  376. static const struct i2c_device_id emc1403_idtable[] = {
  377. { "emc1402", emc1402 },
  378. { "emc1403", emc1403 },
  379. { "emc1404", emc1404 },
  380. { "emc1412", emc1402 },
  381. { "emc1413", emc1403 },
  382. { "emc1414", emc1404 },
  383. { "emc1422", emc1402 },
  384. { "emc1423", emc1403 },
  385. { "emc1424", emc1404 },
  386. { }
  387. };
  388. MODULE_DEVICE_TABLE(i2c, emc1403_idtable);
  389. static struct i2c_driver sensor_emc1403 = {
  390. .class = I2C_CLASS_HWMON,
  391. .driver = {
  392. .name = "emc1403",
  393. },
  394. .detect = emc1403_detect,
  395. .probe_new = emc1403_probe,
  396. .id_table = emc1403_idtable,
  397. .address_list = emc1403_address_list,
  398. };
  399. module_i2c_driver(sensor_emc1403);
  400. MODULE_AUTHOR("Kalhan Trisal <[email protected]");
  401. MODULE_DESCRIPTION("emc1403 Thermal Driver");
  402. MODULE_LICENSE("GPL v2");