0110-hwmon-sfctemp-Add-StarFive-JH71x0-temperature-sensor.patch 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. From 3f68f5d0a8c598a09d26a3908cbbff7312b31487 Mon Sep 17 00:00:00 2001
  2. From: Emil Renner Berthing <[email protected]>
  3. Date: Tue, 21 Mar 2023 10:26:44 +0800
  4. Subject: [PATCH 110/122] hwmon: (sfctemp) Add StarFive JH71x0 temperature
  5. sensor
  6. Add driver for the StarFive JH71x0 temperature sensor. You
  7. can enable/disable it and read temperature in milli Celcius
  8. through sysfs.
  9. Signed-off-by: Emil Renner Berthing <[email protected]>
  10. Co-developed-by: Samin Guo <[email protected]>
  11. Signed-off-by: Samin Guo <[email protected]>
  12. Signed-off-by: Hal Feng <[email protected]>
  13. ---
  14. Documentation/hwmon/index.rst | 1 +
  15. Documentation/hwmon/sfctemp.rst | 33 ++++
  16. MAINTAINERS | 8 +
  17. drivers/hwmon/Kconfig | 10 +
  18. drivers/hwmon/Makefile | 1 +
  19. drivers/hwmon/sfctemp.c | 331 ++++++++++++++++++++++++++++++++
  20. 6 files changed, 384 insertions(+)
  21. create mode 100644 Documentation/hwmon/sfctemp.rst
  22. create mode 100644 drivers/hwmon/sfctemp.c
  23. --- a/Documentation/hwmon/index.rst
  24. +++ b/Documentation/hwmon/index.rst
  25. @@ -179,6 +179,7 @@ Hardware Monitoring Kernel Drivers
  26. sch5627
  27. sch5636
  28. scpi-hwmon
  29. + sfctemp
  30. sht15
  31. sht21
  32. sht3x
  33. --- /dev/null
  34. +++ b/Documentation/hwmon/sfctemp.rst
  35. @@ -0,0 +1,33 @@
  36. +.. SPDX-License-Identifier: GPL-2.0
  37. +
  38. +Kernel driver sfctemp
  39. +=====================
  40. +
  41. +Supported chips:
  42. + - StarFive JH7100
  43. + - StarFive JH7110
  44. +
  45. +Authors:
  46. + - Emil Renner Berthing <[email protected]>
  47. +
  48. +Description
  49. +-----------
  50. +
  51. +This driver adds support for reading the built-in temperature sensor on the
  52. +JH7100 and JH7110 RISC-V SoCs by StarFive Technology Co. Ltd.
  53. +
  54. +``sysfs`` interface
  55. +-------------------
  56. +
  57. +The temperature sensor can be enabled, disabled and queried via the standard
  58. +hwmon interface in sysfs under ``/sys/class/hwmon/hwmonX`` for some value of
  59. +``X``:
  60. +
  61. +================ ==== =============================================
  62. +Name Perm Description
  63. +================ ==== =============================================
  64. +temp1_enable RW Enable or disable temperature sensor.
  65. + Automatically enabled by the driver,
  66. + but may be disabled to save power.
  67. +temp1_input RO Temperature reading in milli-degrees Celsius.
  68. +================ ==== =============================================
  69. --- a/MAINTAINERS
  70. +++ b/MAINTAINERS
  71. @@ -18686,6 +18686,14 @@ L: [email protected]
  72. S: Supported
  73. F: drivers/net/ethernet/sfc/
  74. +SFCTEMP HWMON DRIVER
  75. +M: Emil Renner Berthing <[email protected]>
  76. +L: [email protected]
  77. +S: Maintained
  78. +F: Documentation/devicetree/bindings/hwmon/starfive,jh71x0-temp.yaml
  79. +F: Documentation/hwmon/sfctemp.rst
  80. +F: drivers/hwmon/sfctemp.c
  81. +
  82. SFF/SFP/SFP+ MODULE SUPPORT
  83. M: Russell King <[email protected]>
  84. L: [email protected]
  85. --- a/drivers/hwmon/Kconfig
  86. +++ b/drivers/hwmon/Kconfig
  87. @@ -1911,6 +1911,16 @@ config SENSORS_STTS751
  88. This driver can also be built as a module. If so, the module
  89. will be called stts751.
  90. +config SENSORS_SFCTEMP
  91. + tristate "Starfive JH71x0 temperature sensor"
  92. + depends on ARCH_STARFIVE || COMPILE_TEST
  93. + help
  94. + If you say yes here you get support for temperature sensor
  95. + on the Starfive JH71x0 SoCs.
  96. +
  97. + This driver can also be built as a module. If so, the module
  98. + will be called sfctemp.
  99. +
  100. config SENSORS_SMM665
  101. tristate "Summit Microelectronics SMM665"
  102. depends on I2C
  103. --- a/drivers/hwmon/Makefile
  104. +++ b/drivers/hwmon/Makefile
  105. @@ -179,6 +179,7 @@ obj-$(CONFIG_SENSORS_SBRMI) += sbrmi.o
  106. obj-$(CONFIG_SENSORS_SCH56XX_COMMON)+= sch56xx-common.o
  107. obj-$(CONFIG_SENSORS_SCH5627) += sch5627.o
  108. obj-$(CONFIG_SENSORS_SCH5636) += sch5636.o
  109. +obj-$(CONFIG_SENSORS_SFCTEMP) += sfctemp.o
  110. obj-$(CONFIG_SENSORS_SL28CPLD) += sl28cpld-hwmon.o
  111. obj-$(CONFIG_SENSORS_SHT15) += sht15.o
  112. obj-$(CONFIG_SENSORS_SHT21) += sht21.o
  113. --- /dev/null
  114. +++ b/drivers/hwmon/sfctemp.c
  115. @@ -0,0 +1,331 @@
  116. +// SPDX-License-Identifier: GPL-2.0
  117. +/*
  118. + * Copyright (C) 2021 Emil Renner Berthing <[email protected]>
  119. + * Copyright (C) 2021 Samin Guo <[email protected]>
  120. + */
  121. +
  122. +#include <linux/bits.h>
  123. +#include <linux/clk.h>
  124. +#include <linux/delay.h>
  125. +#include <linux/hwmon.h>
  126. +#include <linux/io.h>
  127. +#include <linux/module.h>
  128. +#include <linux/mutex.h>
  129. +#include <linux/of.h>
  130. +#include <linux/platform_device.h>
  131. +#include <linux/reset.h>
  132. +
  133. +/*
  134. + * TempSensor reset. The RSTN can be de-asserted once the analog core has
  135. + * powered up. Trst(min 100ns)
  136. + * 0:reset 1:de-assert
  137. + */
  138. +#define SFCTEMP_RSTN BIT(0)
  139. +
  140. +/*
  141. + * TempSensor analog core power down. The analog core will be powered up
  142. + * Tpu(min 50us) after PD is de-asserted. RSTN should be held low until the
  143. + * analog core is powered up.
  144. + * 0:power up 1:power down
  145. + */
  146. +#define SFCTEMP_PD BIT(1)
  147. +
  148. +/*
  149. + * TempSensor start conversion enable.
  150. + * 0:disable 1:enable
  151. + */
  152. +#define SFCTEMP_RUN BIT(2)
  153. +
  154. +/*
  155. + * TempSensor conversion value output.
  156. + * Temp(C)=DOUT*Y/4094 - K
  157. + */
  158. +#define SFCTEMP_DOUT_POS 16
  159. +#define SFCTEMP_DOUT_MSK GENMASK(27, 16)
  160. +
  161. +/* DOUT to Celcius conversion constants */
  162. +#define SFCTEMP_Y1000 237500L
  163. +#define SFCTEMP_Z 4094L
  164. +#define SFCTEMP_K1000 81100L
  165. +
  166. +struct sfctemp {
  167. + /* serialize access to hardware register and enabled below */
  168. + struct mutex lock;
  169. + void __iomem *regs;
  170. + struct clk *clk_sense;
  171. + struct clk *clk_bus;
  172. + struct reset_control *rst_sense;
  173. + struct reset_control *rst_bus;
  174. + bool enabled;
  175. +};
  176. +
  177. +static void sfctemp_power_up(struct sfctemp *sfctemp)
  178. +{
  179. + /* make sure we're powered down first */
  180. + writel(SFCTEMP_PD, sfctemp->regs);
  181. + udelay(1);
  182. +
  183. + writel(0, sfctemp->regs);
  184. + /* wait t_pu(50us) + t_rst(100ns) */
  185. + usleep_range(60, 200);
  186. +
  187. + /* de-assert reset */
  188. + writel(SFCTEMP_RSTN, sfctemp->regs);
  189. + udelay(1); /* wait t_su(500ps) */
  190. +}
  191. +
  192. +static void sfctemp_power_down(struct sfctemp *sfctemp)
  193. +{
  194. + writel(SFCTEMP_PD, sfctemp->regs);
  195. +}
  196. +
  197. +static void sfctemp_run(struct sfctemp *sfctemp)
  198. +{
  199. + writel(SFCTEMP_RSTN | SFCTEMP_RUN, sfctemp->regs);
  200. + udelay(1);
  201. +}
  202. +
  203. +static void sfctemp_stop(struct sfctemp *sfctemp)
  204. +{
  205. + writel(SFCTEMP_RSTN, sfctemp->regs);
  206. +}
  207. +
  208. +static int sfctemp_enable(struct sfctemp *sfctemp)
  209. +{
  210. + int ret = 0;
  211. +
  212. + mutex_lock(&sfctemp->lock);
  213. + if (sfctemp->enabled)
  214. + goto done;
  215. +
  216. + ret = clk_prepare_enable(sfctemp->clk_bus);
  217. + if (ret)
  218. + goto err;
  219. + ret = reset_control_deassert(sfctemp->rst_bus);
  220. + if (ret)
  221. + goto err_disable_bus;
  222. +
  223. + ret = clk_prepare_enable(sfctemp->clk_sense);
  224. + if (ret)
  225. + goto err_assert_bus;
  226. + ret = reset_control_deassert(sfctemp->rst_sense);
  227. + if (ret)
  228. + goto err_disable_sense;
  229. +
  230. + sfctemp_power_up(sfctemp);
  231. + sfctemp_run(sfctemp);
  232. + sfctemp->enabled = true;
  233. +done:
  234. + mutex_unlock(&sfctemp->lock);
  235. + return ret;
  236. +
  237. +err_disable_sense:
  238. + clk_disable_unprepare(sfctemp->clk_sense);
  239. +err_assert_bus:
  240. + reset_control_assert(sfctemp->rst_bus);
  241. +err_disable_bus:
  242. + clk_disable_unprepare(sfctemp->clk_bus);
  243. +err:
  244. + mutex_unlock(&sfctemp->lock);
  245. + return ret;
  246. +}
  247. +
  248. +static int sfctemp_disable(struct sfctemp *sfctemp)
  249. +{
  250. + mutex_lock(&sfctemp->lock);
  251. + if (!sfctemp->enabled)
  252. + goto done;
  253. +
  254. + sfctemp_stop(sfctemp);
  255. + sfctemp_power_down(sfctemp);
  256. + reset_control_assert(sfctemp->rst_sense);
  257. + clk_disable_unprepare(sfctemp->clk_sense);
  258. + reset_control_assert(sfctemp->rst_bus);
  259. + clk_disable_unprepare(sfctemp->clk_bus);
  260. + sfctemp->enabled = false;
  261. +done:
  262. + mutex_unlock(&sfctemp->lock);
  263. + return 0;
  264. +}
  265. +
  266. +static void sfctemp_disable_action(void *data)
  267. +{
  268. + sfctemp_disable(data);
  269. +}
  270. +
  271. +static int sfctemp_convert(struct sfctemp *sfctemp, long *val)
  272. +{
  273. + int ret;
  274. +
  275. + mutex_lock(&sfctemp->lock);
  276. + if (!sfctemp->enabled) {
  277. + ret = -ENODATA;
  278. + goto out;
  279. + }
  280. +
  281. + /* calculate temperature in milli Celcius */
  282. + *val = (long)((readl(sfctemp->regs) & SFCTEMP_DOUT_MSK) >> SFCTEMP_DOUT_POS)
  283. + * SFCTEMP_Y1000 / SFCTEMP_Z - SFCTEMP_K1000;
  284. +
  285. + ret = 0;
  286. +out:
  287. + mutex_unlock(&sfctemp->lock);
  288. + return ret;
  289. +}
  290. +
  291. +static umode_t sfctemp_is_visible(const void *data, enum hwmon_sensor_types type,
  292. + u32 attr, int channel)
  293. +{
  294. + switch (type) {
  295. + case hwmon_temp:
  296. + switch (attr) {
  297. + case hwmon_temp_enable:
  298. + return 0644;
  299. + case hwmon_temp_input:
  300. + return 0444;
  301. + default:
  302. + return 0;
  303. + }
  304. + default:
  305. + return 0;
  306. + }
  307. +}
  308. +
  309. +static int sfctemp_read(struct device *dev, enum hwmon_sensor_types type,
  310. + u32 attr, int channel, long *val)
  311. +{
  312. + struct sfctemp *sfctemp = dev_get_drvdata(dev);
  313. +
  314. + switch (type) {
  315. + case hwmon_temp:
  316. + switch (attr) {
  317. + case hwmon_temp_enable:
  318. + *val = sfctemp->enabled;
  319. + return 0;
  320. + case hwmon_temp_input:
  321. + return sfctemp_convert(sfctemp, val);
  322. + default:
  323. + return -EINVAL;
  324. + }
  325. + default:
  326. + return -EINVAL;
  327. + }
  328. +}
  329. +
  330. +static int sfctemp_write(struct device *dev, enum hwmon_sensor_types type,
  331. + u32 attr, int channel, long val)
  332. +{
  333. + struct sfctemp *sfctemp = dev_get_drvdata(dev);
  334. +
  335. + switch (type) {
  336. + case hwmon_temp:
  337. + switch (attr) {
  338. + case hwmon_temp_enable:
  339. + if (val == 0)
  340. + return sfctemp_disable(sfctemp);
  341. + if (val == 1)
  342. + return sfctemp_enable(sfctemp);
  343. + return -EINVAL;
  344. + default:
  345. + return -EINVAL;
  346. + }
  347. + default:
  348. + return -EINVAL;
  349. + }
  350. +}
  351. +
  352. +static const struct hwmon_channel_info *sfctemp_info[] = {
  353. + HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ),
  354. + HWMON_CHANNEL_INFO(temp, HWMON_T_ENABLE | HWMON_T_INPUT),
  355. + NULL
  356. +};
  357. +
  358. +static const struct hwmon_ops sfctemp_hwmon_ops = {
  359. + .is_visible = sfctemp_is_visible,
  360. + .read = sfctemp_read,
  361. + .write = sfctemp_write,
  362. +};
  363. +
  364. +static const struct hwmon_chip_info sfctemp_chip_info = {
  365. + .ops = &sfctemp_hwmon_ops,
  366. + .info = sfctemp_info,
  367. +};
  368. +
  369. +static int sfctemp_probe(struct platform_device *pdev)
  370. +{
  371. + struct device *dev = &pdev->dev;
  372. + struct device *hwmon_dev;
  373. + struct sfctemp *sfctemp;
  374. + int ret;
  375. +
  376. + sfctemp = devm_kzalloc(dev, sizeof(*sfctemp), GFP_KERNEL);
  377. + if (!sfctemp)
  378. + return -ENOMEM;
  379. +
  380. + dev_set_drvdata(dev, sfctemp);
  381. + mutex_init(&sfctemp->lock);
  382. +
  383. + sfctemp->regs = devm_platform_ioremap_resource(pdev, 0);
  384. + if (IS_ERR(sfctemp->regs))
  385. + return PTR_ERR(sfctemp->regs);
  386. +
  387. + sfctemp->clk_sense = devm_clk_get(dev, "sense");
  388. + if (IS_ERR(sfctemp->clk_sense))
  389. + return dev_err_probe(dev, PTR_ERR(sfctemp->clk_sense),
  390. + "error getting sense clock\n");
  391. +
  392. + sfctemp->clk_bus = devm_clk_get(dev, "bus");
  393. + if (IS_ERR(sfctemp->clk_bus))
  394. + return dev_err_probe(dev, PTR_ERR(sfctemp->clk_bus),
  395. + "error getting bus clock\n");
  396. +
  397. + sfctemp->rst_sense = devm_reset_control_get_exclusive(dev, "sense");
  398. + if (IS_ERR(sfctemp->rst_sense))
  399. + return dev_err_probe(dev, PTR_ERR(sfctemp->rst_sense),
  400. + "error getting sense reset\n");
  401. +
  402. + sfctemp->rst_bus = devm_reset_control_get_exclusive(dev, "bus");
  403. + if (IS_ERR(sfctemp->rst_bus))
  404. + return dev_err_probe(dev, PTR_ERR(sfctemp->rst_bus),
  405. + "error getting busreset\n");
  406. +
  407. + ret = reset_control_assert(sfctemp->rst_sense);
  408. + if (ret)
  409. + return dev_err_probe(dev, ret, "error asserting sense reset\n");
  410. +
  411. + ret = reset_control_assert(sfctemp->rst_bus);
  412. + if (ret)
  413. + return dev_err_probe(dev, ret, "error asserting bus reset\n");
  414. +
  415. + ret = devm_add_action(dev, sfctemp_disable_action, sfctemp);
  416. + if (ret)
  417. + return ret;
  418. +
  419. + ret = sfctemp_enable(sfctemp);
  420. + if (ret)
  421. + return dev_err_probe(dev, ret, "error enabling temperature sensor: %d\n", ret);
  422. +
  423. + hwmon_dev = devm_hwmon_device_register_with_info(dev, "sfctemp", sfctemp,
  424. + &sfctemp_chip_info, NULL);
  425. + return PTR_ERR_OR_ZERO(hwmon_dev);
  426. +}
  427. +
  428. +static const struct of_device_id sfctemp_of_match[] = {
  429. + { .compatible = "starfive,jh7100-temp" },
  430. + { .compatible = "starfive,jh7110-temp" },
  431. + { /* sentinel */ }
  432. +};
  433. +MODULE_DEVICE_TABLE(of, sfctemp_of_match);
  434. +
  435. +static struct platform_driver sfctemp_driver = {
  436. + .probe = sfctemp_probe,
  437. + .driver = {
  438. + .name = "sfctemp",
  439. + .of_match_table = sfctemp_of_match,
  440. + },
  441. +};
  442. +module_platform_driver(sfctemp_driver);
  443. +
  444. +MODULE_AUTHOR("Emil Renner Berthing");
  445. +MODULE_DESCRIPTION("StarFive JH71x0 temperature sensor driver");
  446. +MODULE_LICENSE("GPL");