sg2042-mcu.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2024 Inochi Amaoto <inochiama@outlook.com>
  4. *
  5. * Sophgo power control mcu for SG2042
  6. */
  7. #include <linux/cleanup.h>
  8. #include <linux/debugfs.h>
  9. #include <linux/err.h>
  10. #include <linux/hwmon.h>
  11. #include <linux/i2c.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/mutex.h>
  15. /* fixed MCU registers */
  16. #define REG_BOARD_TYPE 0x00
  17. #define REG_MCU_FIRMWARE_VERSION 0x01
  18. #define REG_PCB_VERSION 0x02
  19. #define REG_PWR_CTRL 0x03
  20. #define REG_SOC_TEMP 0x04
  21. #define REG_BOARD_TEMP 0x05
  22. #define REG_RST_COUNT 0x0a
  23. #define REG_UPTIME 0x0b
  24. #define REG_RESET_REASON 0x0d
  25. #define REG_MCU_TYPE 0x18
  26. #define REG_REPOWER_POLICY 0x65
  27. #define REG_CRITICAL_TEMP 0x66
  28. #define REG_REPOWER_TEMP 0x67
  29. #define REPOWER_POLICY_REBOOT 1
  30. #define REPOWER_POLICY_KEEP_OFF 2
  31. #define MCU_POWER_MAX 0xff
  32. #define DEFINE_MCU_DEBUG_ATTR(_name, _reg, _format) \
  33. static int _name##_show(struct seq_file *seqf, \
  34. void *unused) \
  35. { \
  36. struct sg2042_mcu_data *mcu = seqf->private; \
  37. int ret; \
  38. ret = i2c_smbus_read_byte_data(mcu->client, (_reg)); \
  39. if (ret < 0) \
  40. return ret; \
  41. seq_printf(seqf, _format "\n", ret); \
  42. return 0; \
  43. } \
  44. DEFINE_SHOW_ATTRIBUTE(_name) \
  45. struct sg2042_mcu_data {
  46. struct i2c_client *client;
  47. struct mutex mutex;
  48. };
  49. static ssize_t reset_count_show(struct device *dev,
  50. struct device_attribute *attr,
  51. char *buf)
  52. {
  53. struct sg2042_mcu_data *mcu = dev_get_drvdata(dev);
  54. int ret;
  55. ret = i2c_smbus_read_byte_data(mcu->client, REG_RST_COUNT);
  56. if (ret < 0)
  57. return ret;
  58. return sprintf(buf, "%d\n", ret);
  59. }
  60. static ssize_t uptime_show(struct device *dev,
  61. struct device_attribute *attr,
  62. char *buf)
  63. {
  64. struct sg2042_mcu_data *mcu = dev_get_drvdata(dev);
  65. u8 time_val[2];
  66. int ret;
  67. ret = i2c_smbus_read_i2c_block_data(mcu->client, REG_UPTIME,
  68. sizeof(time_val), time_val);
  69. if (ret < 0)
  70. return ret;
  71. return sprintf(buf, "%d\n",
  72. (time_val[0]) | (time_val[1] << 8));
  73. }
  74. static ssize_t reset_reason_show(struct device *dev,
  75. struct device_attribute *attr,
  76. char *buf)
  77. {
  78. struct sg2042_mcu_data *mcu = dev_get_drvdata(dev);
  79. int ret;
  80. ret = i2c_smbus_read_byte_data(mcu->client, REG_RESET_REASON);
  81. if (ret < 0)
  82. return ret;
  83. return sprintf(buf, "0x%02x\n", ret);
  84. }
  85. static ssize_t repower_policy_show(struct device *dev,
  86. struct device_attribute *attr,
  87. char *buf)
  88. {
  89. struct sg2042_mcu_data *mcu = dev_get_drvdata(dev);
  90. int ret;
  91. const char *action;
  92. ret = i2c_smbus_read_byte_data(mcu->client, REG_REPOWER_POLICY);
  93. if (ret < 0)
  94. return ret;
  95. if (ret == REPOWER_POLICY_REBOOT)
  96. action = "repower";
  97. else if (ret == REPOWER_POLICY_KEEP_OFF)
  98. action = "keep";
  99. else
  100. action = "unknown";
  101. return sprintf(buf, "%s\n", action);
  102. }
  103. static ssize_t repower_policy_store(struct device *dev,
  104. struct device_attribute *attr,
  105. const char *buf, size_t count)
  106. {
  107. struct sg2042_mcu_data *mcu = dev_get_drvdata(dev);
  108. u8 value;
  109. int ret;
  110. if (sysfs_streq("repower", buf))
  111. value = REPOWER_POLICY_REBOOT;
  112. else if (sysfs_streq("keep", buf))
  113. value = REPOWER_POLICY_KEEP_OFF;
  114. else
  115. return -EINVAL;
  116. ret = i2c_smbus_write_byte_data(mcu->client,
  117. REG_REPOWER_POLICY, value);
  118. if (ret < 0)
  119. return ret;
  120. return count;
  121. }
  122. static DEVICE_ATTR_RO(reset_count);
  123. static DEVICE_ATTR_RO(uptime);
  124. static DEVICE_ATTR_RO(reset_reason);
  125. static DEVICE_ATTR_RW(repower_policy);
  126. DEFINE_MCU_DEBUG_ATTR(firmware_version, REG_MCU_FIRMWARE_VERSION, "0x%02x");
  127. DEFINE_MCU_DEBUG_ATTR(pcb_version, REG_PCB_VERSION, "0x%02x");
  128. DEFINE_MCU_DEBUG_ATTR(board_type, REG_BOARD_TYPE, "0x%02x");
  129. DEFINE_MCU_DEBUG_ATTR(mcu_type, REG_MCU_TYPE, "%d");
  130. static struct attribute *sg2042_mcu_attrs[] = {
  131. &dev_attr_reset_count.attr,
  132. &dev_attr_uptime.attr,
  133. &dev_attr_reset_reason.attr,
  134. &dev_attr_repower_policy.attr,
  135. NULL
  136. };
  137. static const struct attribute_group sg2042_mcu_attr_group = {
  138. .attrs = sg2042_mcu_attrs,
  139. };
  140. static const struct attribute_group *sg2042_mcu_groups[] = {
  141. &sg2042_mcu_attr_group,
  142. NULL
  143. };
  144. static const struct hwmon_channel_info * const sg2042_mcu_info[] = {
  145. HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ),
  146. HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_CRIT |
  147. HWMON_T_CRIT_HYST,
  148. HWMON_T_INPUT),
  149. NULL
  150. };
  151. static int sg2042_mcu_read(struct device *dev,
  152. enum hwmon_sensor_types type,
  153. u32 attr, int channel, long *val)
  154. {
  155. struct sg2042_mcu_data *mcu = dev_get_drvdata(dev);
  156. int tmp;
  157. u8 reg;
  158. switch (attr) {
  159. case hwmon_temp_input:
  160. reg = channel ? REG_BOARD_TEMP : REG_SOC_TEMP;
  161. break;
  162. case hwmon_temp_crit:
  163. reg = REG_CRITICAL_TEMP;
  164. break;
  165. case hwmon_temp_crit_hyst:
  166. reg = REG_REPOWER_TEMP;
  167. break;
  168. default:
  169. return -EOPNOTSUPP;
  170. }
  171. tmp = i2c_smbus_read_byte_data(mcu->client, reg);
  172. if (tmp < 0)
  173. return tmp;
  174. *val = tmp * 1000;
  175. return 0;
  176. }
  177. static int sg2042_mcu_write(struct device *dev,
  178. enum hwmon_sensor_types type,
  179. u32 attr, int channel, long val)
  180. {
  181. struct sg2042_mcu_data *mcu = dev_get_drvdata(dev);
  182. int temp = val / 1000;
  183. int hyst_temp, crit_temp;
  184. u8 reg;
  185. temp = clamp_val(temp, 0, MCU_POWER_MAX);
  186. guard(mutex)(&mcu->mutex);
  187. switch (attr) {
  188. case hwmon_temp_crit:
  189. hyst_temp = i2c_smbus_read_byte_data(mcu->client,
  190. REG_REPOWER_TEMP);
  191. if (hyst_temp < 0)
  192. return hyst_temp;
  193. crit_temp = temp;
  194. reg = REG_CRITICAL_TEMP;
  195. break;
  196. case hwmon_temp_crit_hyst:
  197. crit_temp = i2c_smbus_read_byte_data(mcu->client,
  198. REG_CRITICAL_TEMP);
  199. if (crit_temp < 0)
  200. return crit_temp;
  201. hyst_temp = temp;
  202. reg = REG_REPOWER_TEMP;
  203. break;
  204. default:
  205. return -EOPNOTSUPP;
  206. }
  207. /*
  208. * ensure hyst_temp is smaller to avoid MCU from
  209. * keeping triggering repower event.
  210. */
  211. if (crit_temp < hyst_temp)
  212. return -EINVAL;
  213. return i2c_smbus_write_byte_data(mcu->client, reg, temp);
  214. }
  215. static umode_t sg2042_mcu_is_visible(const void *_data,
  216. enum hwmon_sensor_types type,
  217. u32 attr, int channel)
  218. {
  219. switch (type) {
  220. case hwmon_temp:
  221. switch (attr) {
  222. case hwmon_temp_input:
  223. return 0444;
  224. case hwmon_temp_crit:
  225. case hwmon_temp_crit_hyst:
  226. if (channel == 0)
  227. return 0644;
  228. break;
  229. default:
  230. break;
  231. }
  232. break;
  233. default:
  234. break;
  235. }
  236. return 0;
  237. }
  238. static const struct hwmon_ops sg2042_mcu_ops = {
  239. .is_visible = sg2042_mcu_is_visible,
  240. .read = sg2042_mcu_read,
  241. .write = sg2042_mcu_write,
  242. };
  243. static const struct hwmon_chip_info sg2042_mcu_chip_info = {
  244. .ops = &sg2042_mcu_ops,
  245. .info = sg2042_mcu_info,
  246. };
  247. static void sg2042_mcu_debugfs_init(struct sg2042_mcu_data *mcu)
  248. {
  249. debugfs_create_file("firmware_version", 0444, mcu->client->debugfs,
  250. mcu, &firmware_version_fops);
  251. debugfs_create_file("pcb_version", 0444, mcu->client->debugfs, mcu,
  252. &pcb_version_fops);
  253. debugfs_create_file("mcu_type", 0444, mcu->client->debugfs, mcu,
  254. &mcu_type_fops);
  255. debugfs_create_file("board_type", 0444, mcu->client->debugfs, mcu,
  256. &board_type_fops);
  257. }
  258. static int sg2042_mcu_i2c_probe(struct i2c_client *client)
  259. {
  260. struct device *dev = &client->dev;
  261. struct sg2042_mcu_data *mcu;
  262. struct device *hwmon_dev;
  263. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  264. I2C_FUNC_SMBUS_BLOCK_DATA))
  265. return -ENODEV;
  266. mcu = devm_kmalloc(dev, sizeof(*mcu), GFP_KERNEL);
  267. if (!mcu)
  268. return -ENOMEM;
  269. mutex_init(&mcu->mutex);
  270. mcu->client = client;
  271. i2c_set_clientdata(client, mcu);
  272. hwmon_dev = devm_hwmon_device_register_with_info(dev, "sg2042_mcu",
  273. mcu,
  274. &sg2042_mcu_chip_info,
  275. NULL);
  276. if (IS_ERR(hwmon_dev))
  277. return PTR_ERR(hwmon_dev);
  278. sg2042_mcu_debugfs_init(mcu);
  279. return 0;
  280. }
  281. static const struct i2c_device_id sg2042_mcu_id[] = {
  282. { "sg2042-hwmon-mcu" },
  283. { }
  284. };
  285. MODULE_DEVICE_TABLE(i2c, sg2042_mcu_id);
  286. static const struct of_device_id sg2042_mcu_of_id[] = {
  287. { .compatible = "sophgo,sg2042-hwmon-mcu" },
  288. {},
  289. };
  290. MODULE_DEVICE_TABLE(of, sg2042_mcu_of_id);
  291. static struct i2c_driver sg2042_mcu_driver = {
  292. .driver = {
  293. .name = "sg2042-mcu",
  294. .of_match_table = sg2042_mcu_of_id,
  295. .dev_groups = sg2042_mcu_groups,
  296. },
  297. .probe = sg2042_mcu_i2c_probe,
  298. .id_table = sg2042_mcu_id,
  299. };
  300. module_i2c_driver(sg2042_mcu_driver);
  301. MODULE_AUTHOR("Inochi Amaoto <inochiama@outlook.com>");
  302. MODULE_DESCRIPTION("MCU I2C driver for SG2042 soc platform");
  303. MODULE_LICENSE("GPL");