armada_thermal.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. /*
  2. * Marvell EBU Armada SoCs thermal sensor driver
  3. *
  4. * Copyright (C) 2013 Marvell
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. */
  16. #include <linux/device.h>
  17. #include <linux/err.h>
  18. #include <linux/io.h>
  19. #include <linux/kernel.h>
  20. #include <linux/of.h>
  21. #include <linux/module.h>
  22. #include <linux/delay.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/of_device.h>
  25. #include <linux/thermal.h>
  26. #include <linux/iopoll.h>
  27. #include <linux/mfd/syscon.h>
  28. #include <linux/regmap.h>
  29. /* Thermal Manager Control and Status Register */
  30. #define PMU_TDC0_SW_RST_MASK (0x1 << 1)
  31. #define PMU_TM_DISABLE_OFFS 0
  32. #define PMU_TM_DISABLE_MASK (0x1 << PMU_TM_DISABLE_OFFS)
  33. #define PMU_TDC0_REF_CAL_CNT_OFFS 11
  34. #define PMU_TDC0_REF_CAL_CNT_MASK (0x1ff << PMU_TDC0_REF_CAL_CNT_OFFS)
  35. #define PMU_TDC0_OTF_CAL_MASK (0x1 << 30)
  36. #define PMU_TDC0_START_CAL_MASK (0x1 << 25)
  37. #define A375_UNIT_CONTROL_SHIFT 27
  38. #define A375_UNIT_CONTROL_MASK 0x7
  39. #define A375_READOUT_INVERT BIT(15)
  40. #define A375_HW_RESETn BIT(8)
  41. /* Errata fields */
  42. #define CONTROL0_TSEN_TC_TRIM_MASK 0x7
  43. #define CONTROL0_TSEN_TC_TRIM_VAL 0x3
  44. #define CONTROL0_TSEN_START BIT(0)
  45. #define CONTROL0_TSEN_RESET BIT(1)
  46. #define CONTROL0_TSEN_ENABLE BIT(2)
  47. #define CONTROL0_TSEN_AVG_BYPASS BIT(6)
  48. #define CONTROL0_TSEN_CHAN_SHIFT 13
  49. #define CONTROL0_TSEN_CHAN_MASK 0xF
  50. #define CONTROL0_TSEN_OSR_SHIFT 24
  51. #define CONTROL0_TSEN_OSR_MAX 0x3
  52. #define CONTROL0_TSEN_MODE_SHIFT 30
  53. #define CONTROL0_TSEN_MODE_EXTERNAL 0x2
  54. #define CONTROL0_TSEN_MODE_MASK 0x3
  55. #define CONTROL1_TSEN_AVG_SHIFT 0
  56. #define CONTROL1_TSEN_AVG_MASK 0x7
  57. #define CONTROL1_EXT_TSEN_SW_RESET BIT(7)
  58. #define CONTROL1_EXT_TSEN_HW_RESETn BIT(8)
  59. #define STATUS_POLL_PERIOD_US 1000
  60. #define STATUS_POLL_TIMEOUT_US 100000
  61. struct armada_thermal_data;
  62. /* Marvell EBU Thermal Sensor Dev Structure */
  63. struct armada_thermal_priv {
  64. struct device *dev;
  65. struct regmap *syscon;
  66. char zone_name[THERMAL_NAME_LENGTH];
  67. /* serialize temperature reads/updates */
  68. struct mutex update_lock;
  69. struct armada_thermal_data *data;
  70. int current_channel;
  71. };
  72. struct armada_thermal_data {
  73. /* Initialize the thermal IC */
  74. void (*init)(struct platform_device *pdev,
  75. struct armada_thermal_priv *priv);
  76. /* Formula coeficients: temp = (b - m * reg) / div */
  77. s64 coef_b;
  78. s64 coef_m;
  79. u32 coef_div;
  80. bool inverted;
  81. bool signed_sample;
  82. /* Register shift and mask to access the sensor temperature */
  83. unsigned int temp_shift;
  84. unsigned int temp_mask;
  85. u32 is_valid_bit;
  86. /* Syscon access */
  87. unsigned int syscon_control0_off;
  88. unsigned int syscon_control1_off;
  89. unsigned int syscon_status_off;
  90. /* One sensor is in the thermal IC, the others are in the CPUs if any */
  91. unsigned int cpu_nr;
  92. };
  93. struct armada_drvdata {
  94. enum drvtype {
  95. LEGACY,
  96. SYSCON
  97. } type;
  98. union {
  99. struct armada_thermal_priv *priv;
  100. struct thermal_zone_device *tz;
  101. } data;
  102. };
  103. /*
  104. * struct armada_thermal_sensor - hold the information of one thermal sensor
  105. * @thermal: pointer to the local private structure
  106. * @tzd: pointer to the thermal zone device
  107. * @id: identifier of the thermal sensor
  108. */
  109. struct armada_thermal_sensor {
  110. struct armada_thermal_priv *priv;
  111. int id;
  112. };
  113. static void armadaxp_init(struct platform_device *pdev,
  114. struct armada_thermal_priv *priv)
  115. {
  116. struct armada_thermal_data *data = priv->data;
  117. u32 reg;
  118. regmap_read(priv->syscon, data->syscon_control1_off, &reg);
  119. reg |= PMU_TDC0_OTF_CAL_MASK;
  120. /* Reference calibration value */
  121. reg &= ~PMU_TDC0_REF_CAL_CNT_MASK;
  122. reg |= (0xf1 << PMU_TDC0_REF_CAL_CNT_OFFS);
  123. /* Reset the sensor */
  124. reg |= PMU_TDC0_SW_RST_MASK;
  125. regmap_write(priv->syscon, data->syscon_control1_off, reg);
  126. /* Enable the sensor */
  127. regmap_read(priv->syscon, data->syscon_status_off, &reg);
  128. reg &= ~PMU_TM_DISABLE_MASK;
  129. regmap_write(priv->syscon, data->syscon_status_off, reg);
  130. }
  131. static void armada370_init(struct platform_device *pdev,
  132. struct armada_thermal_priv *priv)
  133. {
  134. struct armada_thermal_data *data = priv->data;
  135. u32 reg;
  136. regmap_read(priv->syscon, data->syscon_control1_off, &reg);
  137. reg |= PMU_TDC0_OTF_CAL_MASK;
  138. /* Reference calibration value */
  139. reg &= ~PMU_TDC0_REF_CAL_CNT_MASK;
  140. reg |= (0xf1 << PMU_TDC0_REF_CAL_CNT_OFFS);
  141. /* Reset the sensor */
  142. reg &= ~PMU_TDC0_START_CAL_MASK;
  143. regmap_write(priv->syscon, data->syscon_control1_off, reg);
  144. msleep(10);
  145. }
  146. static void armada375_init(struct platform_device *pdev,
  147. struct armada_thermal_priv *priv)
  148. {
  149. struct armada_thermal_data *data = priv->data;
  150. u32 reg;
  151. regmap_read(priv->syscon, data->syscon_control1_off, &reg);
  152. reg &= ~(A375_UNIT_CONTROL_MASK << A375_UNIT_CONTROL_SHIFT);
  153. reg &= ~A375_READOUT_INVERT;
  154. reg &= ~A375_HW_RESETn;
  155. regmap_write(priv->syscon, data->syscon_control1_off, reg);
  156. msleep(20);
  157. reg |= A375_HW_RESETn;
  158. regmap_write(priv->syscon, data->syscon_control1_off, reg);
  159. msleep(50);
  160. }
  161. static int armada_wait_sensor_validity(struct armada_thermal_priv *priv)
  162. {
  163. u32 reg;
  164. return regmap_read_poll_timeout(priv->syscon,
  165. priv->data->syscon_status_off, reg,
  166. reg & priv->data->is_valid_bit,
  167. STATUS_POLL_PERIOD_US,
  168. STATUS_POLL_TIMEOUT_US);
  169. }
  170. static void armada380_init(struct platform_device *pdev,
  171. struct armada_thermal_priv *priv)
  172. {
  173. struct armada_thermal_data *data = priv->data;
  174. u32 reg;
  175. /* Disable the HW/SW reset */
  176. regmap_read(priv->syscon, data->syscon_control1_off, &reg);
  177. reg |= CONTROL1_EXT_TSEN_HW_RESETn;
  178. reg &= ~CONTROL1_EXT_TSEN_SW_RESET;
  179. regmap_write(priv->syscon, data->syscon_control1_off, reg);
  180. /* Set Tsen Tc Trim to correct default value (errata #132698) */
  181. regmap_read(priv->syscon, data->syscon_control0_off, &reg);
  182. reg &= ~CONTROL0_TSEN_TC_TRIM_MASK;
  183. reg |= CONTROL0_TSEN_TC_TRIM_VAL;
  184. regmap_write(priv->syscon, data->syscon_control0_off, reg);
  185. }
  186. static void armada_ap806_init(struct platform_device *pdev,
  187. struct armada_thermal_priv *priv)
  188. {
  189. struct armada_thermal_data *data = priv->data;
  190. u32 reg;
  191. regmap_read(priv->syscon, data->syscon_control0_off, &reg);
  192. reg &= ~CONTROL0_TSEN_RESET;
  193. reg |= CONTROL0_TSEN_START | CONTROL0_TSEN_ENABLE;
  194. /* Sample every ~2ms */
  195. reg |= CONTROL0_TSEN_OSR_MAX << CONTROL0_TSEN_OSR_SHIFT;
  196. /* Enable average (2 samples by default) */
  197. reg &= ~CONTROL0_TSEN_AVG_BYPASS;
  198. regmap_write(priv->syscon, data->syscon_control0_off, reg);
  199. }
  200. static void armada_cp110_init(struct platform_device *pdev,
  201. struct armada_thermal_priv *priv)
  202. {
  203. struct armada_thermal_data *data = priv->data;
  204. u32 reg;
  205. armada380_init(pdev, priv);
  206. /* Sample every ~2ms */
  207. regmap_read(priv->syscon, data->syscon_control0_off, &reg);
  208. reg |= CONTROL0_TSEN_OSR_MAX << CONTROL0_TSEN_OSR_SHIFT;
  209. regmap_write(priv->syscon, data->syscon_control0_off, reg);
  210. /* Average the output value over 2^1 = 2 samples */
  211. regmap_read(priv->syscon, data->syscon_control1_off, &reg);
  212. reg &= ~CONTROL1_TSEN_AVG_MASK << CONTROL1_TSEN_AVG_SHIFT;
  213. reg |= 1 << CONTROL1_TSEN_AVG_SHIFT;
  214. regmap_write(priv->syscon, data->syscon_control1_off, reg);
  215. }
  216. static bool armada_is_valid(struct armada_thermal_priv *priv)
  217. {
  218. u32 reg;
  219. if (!priv->data->is_valid_bit)
  220. return true;
  221. regmap_read(priv->syscon, priv->data->syscon_status_off, &reg);
  222. return reg & priv->data->is_valid_bit;
  223. }
  224. /* There is currently no board with more than one sensor per channel */
  225. static int armada_select_channel(struct armada_thermal_priv *priv, int channel)
  226. {
  227. struct armada_thermal_data *data = priv->data;
  228. u32 ctrl0;
  229. if (channel < 0 || channel > priv->data->cpu_nr)
  230. return -EINVAL;
  231. if (priv->current_channel == channel)
  232. return 0;
  233. /* Stop the measurements */
  234. regmap_read(priv->syscon, data->syscon_control0_off, &ctrl0);
  235. ctrl0 &= ~CONTROL0_TSEN_START;
  236. regmap_write(priv->syscon, data->syscon_control0_off, ctrl0);
  237. /* Reset the mode, internal sensor will be automatically selected */
  238. ctrl0 &= ~(CONTROL0_TSEN_MODE_MASK << CONTROL0_TSEN_MODE_SHIFT);
  239. /* Other channels are external and should be selected accordingly */
  240. if (channel) {
  241. /* Change the mode to external */
  242. ctrl0 |= CONTROL0_TSEN_MODE_EXTERNAL <<
  243. CONTROL0_TSEN_MODE_SHIFT;
  244. /* Select the sensor */
  245. ctrl0 &= ~(CONTROL0_TSEN_CHAN_MASK << CONTROL0_TSEN_CHAN_SHIFT);
  246. ctrl0 |= (channel - 1) << CONTROL0_TSEN_CHAN_SHIFT;
  247. }
  248. /* Actually set the mode/channel */
  249. regmap_write(priv->syscon, data->syscon_control0_off, ctrl0);
  250. priv->current_channel = channel;
  251. /* Re-start the measurements */
  252. ctrl0 |= CONTROL0_TSEN_START;
  253. regmap_write(priv->syscon, data->syscon_control0_off, ctrl0);
  254. /*
  255. * The IP has a latency of ~15ms, so after updating the selected source,
  256. * we must absolutely wait for the sensor validity bit to ensure we read
  257. * actual data.
  258. */
  259. if (armada_wait_sensor_validity(priv)) {
  260. dev_err(priv->dev,
  261. "Temperature sensor reading not valid\n");
  262. return -EIO;
  263. }
  264. return 0;
  265. }
  266. static int armada_read_sensor(struct armada_thermal_priv *priv, int *temp)
  267. {
  268. u32 reg, div;
  269. s64 sample, b, m;
  270. regmap_read(priv->syscon, priv->data->syscon_status_off, &reg);
  271. reg = (reg >> priv->data->temp_shift) & priv->data->temp_mask;
  272. if (priv->data->signed_sample)
  273. /* The most significant bit is the sign bit */
  274. sample = sign_extend32(reg, fls(priv->data->temp_mask) - 1);
  275. else
  276. sample = reg;
  277. /* Get formula coeficients */
  278. b = priv->data->coef_b;
  279. m = priv->data->coef_m;
  280. div = priv->data->coef_div;
  281. if (priv->data->inverted)
  282. *temp = div_s64((m * sample) - b, div);
  283. else
  284. *temp = div_s64(b - (m * sample), div);
  285. return 0;
  286. }
  287. static int armada_get_temp_legacy(struct thermal_zone_device *thermal,
  288. int *temp)
  289. {
  290. struct armada_thermal_priv *priv = thermal->devdata;
  291. int ret;
  292. /* Valid check */
  293. if (!armada_is_valid(priv)) {
  294. dev_err(priv->dev,
  295. "Temperature sensor reading not valid\n");
  296. return -EIO;
  297. }
  298. /* Do the actual reading */
  299. ret = armada_read_sensor(priv, temp);
  300. return ret;
  301. }
  302. static struct thermal_zone_device_ops legacy_ops = {
  303. .get_temp = armada_get_temp_legacy,
  304. };
  305. static int armada_get_temp(void *_sensor, int *temp)
  306. {
  307. struct armada_thermal_sensor *sensor = _sensor;
  308. struct armada_thermal_priv *priv = sensor->priv;
  309. int ret;
  310. mutex_lock(&priv->update_lock);
  311. /* Select the desired channel */
  312. ret = armada_select_channel(priv, sensor->id);
  313. if (ret)
  314. goto unlock_mutex;
  315. /* Do the actual reading */
  316. ret = armada_read_sensor(priv, temp);
  317. unlock_mutex:
  318. mutex_unlock(&priv->update_lock);
  319. return ret;
  320. }
  321. static struct thermal_zone_of_device_ops of_ops = {
  322. .get_temp = armada_get_temp,
  323. };
  324. static const struct armada_thermal_data armadaxp_data = {
  325. .init = armadaxp_init,
  326. .temp_shift = 10,
  327. .temp_mask = 0x1ff,
  328. .coef_b = 3153000000ULL,
  329. .coef_m = 10000000ULL,
  330. .coef_div = 13825,
  331. .syscon_status_off = 0xb0,
  332. .syscon_control1_off = 0xd0,
  333. };
  334. static const struct armada_thermal_data armada370_data = {
  335. .init = armada370_init,
  336. .is_valid_bit = BIT(9),
  337. .temp_shift = 10,
  338. .temp_mask = 0x1ff,
  339. .coef_b = 3153000000ULL,
  340. .coef_m = 10000000ULL,
  341. .coef_div = 13825,
  342. .syscon_status_off = 0x0,
  343. .syscon_control1_off = 0x4,
  344. };
  345. static const struct armada_thermal_data armada375_data = {
  346. .init = armada375_init,
  347. .is_valid_bit = BIT(10),
  348. .temp_shift = 0,
  349. .temp_mask = 0x1ff,
  350. .coef_b = 3171900000ULL,
  351. .coef_m = 10000000ULL,
  352. .coef_div = 13616,
  353. .syscon_status_off = 0x78,
  354. .syscon_control0_off = 0x7c,
  355. .syscon_control1_off = 0x80,
  356. };
  357. static const struct armada_thermal_data armada380_data = {
  358. .init = armada380_init,
  359. .is_valid_bit = BIT(10),
  360. .temp_shift = 0,
  361. .temp_mask = 0x3ff,
  362. .coef_b = 1172499100ULL,
  363. .coef_m = 2000096ULL,
  364. .coef_div = 4201,
  365. .inverted = true,
  366. .syscon_control0_off = 0x70,
  367. .syscon_control1_off = 0x74,
  368. .syscon_status_off = 0x78,
  369. };
  370. static const struct armada_thermal_data armada_ap806_data = {
  371. .init = armada_ap806_init,
  372. .is_valid_bit = BIT(16),
  373. .temp_shift = 0,
  374. .temp_mask = 0x3ff,
  375. .coef_b = -150000LL,
  376. .coef_m = 423ULL,
  377. .coef_div = 1,
  378. .inverted = true,
  379. .signed_sample = true,
  380. .syscon_control0_off = 0x84,
  381. .syscon_control1_off = 0x88,
  382. .syscon_status_off = 0x8C,
  383. .cpu_nr = 4,
  384. };
  385. static const struct armada_thermal_data armada_cp110_data = {
  386. .init = armada_cp110_init,
  387. .is_valid_bit = BIT(10),
  388. .temp_shift = 0,
  389. .temp_mask = 0x3ff,
  390. .coef_b = 1172499100ULL,
  391. .coef_m = 2000096ULL,
  392. .coef_div = 4201,
  393. .inverted = true,
  394. .syscon_control0_off = 0x70,
  395. .syscon_control1_off = 0x74,
  396. .syscon_status_off = 0x78,
  397. };
  398. static const struct of_device_id armada_thermal_id_table[] = {
  399. {
  400. .compatible = "marvell,armadaxp-thermal",
  401. .data = &armadaxp_data,
  402. },
  403. {
  404. .compatible = "marvell,armada370-thermal",
  405. .data = &armada370_data,
  406. },
  407. {
  408. .compatible = "marvell,armada375-thermal",
  409. .data = &armada375_data,
  410. },
  411. {
  412. .compatible = "marvell,armada380-thermal",
  413. .data = &armada380_data,
  414. },
  415. {
  416. .compatible = "marvell,armada-ap806-thermal",
  417. .data = &armada_ap806_data,
  418. },
  419. {
  420. .compatible = "marvell,armada-cp110-thermal",
  421. .data = &armada_cp110_data,
  422. },
  423. {
  424. /* sentinel */
  425. },
  426. };
  427. MODULE_DEVICE_TABLE(of, armada_thermal_id_table);
  428. static const struct regmap_config armada_thermal_regmap_config = {
  429. .reg_bits = 32,
  430. .reg_stride = 4,
  431. .val_bits = 32,
  432. .fast_io = true,
  433. };
  434. static int armada_thermal_probe_legacy(struct platform_device *pdev,
  435. struct armada_thermal_priv *priv)
  436. {
  437. struct armada_thermal_data *data = priv->data;
  438. struct resource *res;
  439. void __iomem *base;
  440. /* First memory region points towards the status register */
  441. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  442. if (!res)
  443. return -EIO;
  444. /*
  445. * Edit the resource start address and length to map over all the
  446. * registers, instead of pointing at them one by one.
  447. */
  448. res->start -= data->syscon_status_off;
  449. res->end = res->start + max(data->syscon_status_off,
  450. max(data->syscon_control0_off,
  451. data->syscon_control1_off)) +
  452. sizeof(unsigned int) - 1;
  453. base = devm_ioremap_resource(&pdev->dev, res);
  454. if (IS_ERR(base))
  455. return PTR_ERR(base);
  456. priv->syscon = devm_regmap_init_mmio(&pdev->dev, base,
  457. &armada_thermal_regmap_config);
  458. if (IS_ERR(priv->syscon))
  459. return PTR_ERR(priv->syscon);
  460. return 0;
  461. }
  462. static int armada_thermal_probe_syscon(struct platform_device *pdev,
  463. struct armada_thermal_priv *priv)
  464. {
  465. priv->syscon = syscon_node_to_regmap(pdev->dev.parent->of_node);
  466. if (IS_ERR(priv->syscon))
  467. return PTR_ERR(priv->syscon);
  468. return 0;
  469. }
  470. static void armada_set_sane_name(struct platform_device *pdev,
  471. struct armada_thermal_priv *priv)
  472. {
  473. const char *name = dev_name(&pdev->dev);
  474. char *insane_char;
  475. if (strlen(name) > THERMAL_NAME_LENGTH) {
  476. /*
  477. * When inside a system controller, the device name has the
  478. * form: f06f8000.system-controller:ap-thermal so stripping
  479. * after the ':' should give us a shorter but meaningful name.
  480. */
  481. name = strrchr(name, ':');
  482. if (!name)
  483. name = "armada_thermal";
  484. else
  485. name++;
  486. }
  487. /* Save the name locally */
  488. strncpy(priv->zone_name, name, THERMAL_NAME_LENGTH - 1);
  489. priv->zone_name[THERMAL_NAME_LENGTH - 1] = '\0';
  490. /* Then check there are no '-' or hwmon core will complain */
  491. do {
  492. insane_char = strpbrk(priv->zone_name, "-");
  493. if (insane_char)
  494. *insane_char = '_';
  495. } while (insane_char);
  496. }
  497. static int armada_thermal_probe(struct platform_device *pdev)
  498. {
  499. struct thermal_zone_device *tz;
  500. struct armada_thermal_sensor *sensor;
  501. struct armada_drvdata *drvdata;
  502. const struct of_device_id *match;
  503. struct armada_thermal_priv *priv;
  504. int sensor_id;
  505. int ret;
  506. match = of_match_device(armada_thermal_id_table, &pdev->dev);
  507. if (!match)
  508. return -ENODEV;
  509. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  510. if (!priv)
  511. return -ENOMEM;
  512. drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
  513. if (!drvdata)
  514. return -ENOMEM;
  515. priv->dev = &pdev->dev;
  516. priv->data = (struct armada_thermal_data *)match->data;
  517. mutex_init(&priv->update_lock);
  518. /*
  519. * Legacy DT bindings only described "control1" register (also referred
  520. * as "control MSB" on old documentation). Then, bindings moved to cover
  521. * "control0/control LSB" and "control1/control MSB" registers within
  522. * the same resource, which was then of size 8 instead of 4.
  523. *
  524. * The logic of defining sporadic registers is broken. For instance, it
  525. * blocked the addition of the overheat interrupt feature that needed
  526. * another resource somewhere else in the same memory area. One solution
  527. * is to define an overall system controller and put the thermal node
  528. * into it, which requires the use of regmaps across all the driver.
  529. */
  530. if (IS_ERR(syscon_node_to_regmap(pdev->dev.parent->of_node))) {
  531. /* Ensure device name is correct for the thermal core */
  532. armada_set_sane_name(pdev, priv);
  533. ret = armada_thermal_probe_legacy(pdev, priv);
  534. if (ret)
  535. return ret;
  536. priv->data->init(pdev, priv);
  537. /* Wait the sensors to be valid */
  538. armada_wait_sensor_validity(priv);
  539. tz = thermal_zone_device_register(priv->zone_name, 0, 0, priv,
  540. &legacy_ops, NULL, 0, 0);
  541. if (IS_ERR(tz)) {
  542. dev_err(&pdev->dev,
  543. "Failed to register thermal zone device\n");
  544. return PTR_ERR(tz);
  545. }
  546. drvdata->type = LEGACY;
  547. drvdata->data.tz = tz;
  548. platform_set_drvdata(pdev, drvdata);
  549. return 0;
  550. }
  551. ret = armada_thermal_probe_syscon(pdev, priv);
  552. if (ret)
  553. return ret;
  554. priv->current_channel = -1;
  555. priv->data->init(pdev, priv);
  556. drvdata->type = SYSCON;
  557. drvdata->data.priv = priv;
  558. platform_set_drvdata(pdev, drvdata);
  559. /*
  560. * There is one channel for the IC and one per CPU (if any), each
  561. * channel has one sensor.
  562. */
  563. for (sensor_id = 0; sensor_id <= priv->data->cpu_nr; sensor_id++) {
  564. sensor = devm_kzalloc(&pdev->dev,
  565. sizeof(struct armada_thermal_sensor),
  566. GFP_KERNEL);
  567. if (!sensor)
  568. return -ENOMEM;
  569. /* Register the sensor */
  570. sensor->priv = priv;
  571. sensor->id = sensor_id;
  572. tz = devm_thermal_zone_of_sensor_register(&pdev->dev,
  573. sensor->id, sensor,
  574. &of_ops);
  575. if (IS_ERR(tz)) {
  576. dev_info(&pdev->dev, "Thermal sensor %d unavailable\n",
  577. sensor_id);
  578. devm_kfree(&pdev->dev, sensor);
  579. continue;
  580. }
  581. }
  582. return 0;
  583. }
  584. static int armada_thermal_exit(struct platform_device *pdev)
  585. {
  586. struct armada_drvdata *drvdata = platform_get_drvdata(pdev);
  587. if (drvdata->type == LEGACY)
  588. thermal_zone_device_unregister(drvdata->data.tz);
  589. return 0;
  590. }
  591. static struct platform_driver armada_thermal_driver = {
  592. .probe = armada_thermal_probe,
  593. .remove = armada_thermal_exit,
  594. .driver = {
  595. .name = "armada_thermal",
  596. .of_match_table = armada_thermal_id_table,
  597. },
  598. };
  599. module_platform_driver(armada_thermal_driver);
  600. MODULE_AUTHOR("Ezequiel Garcia <ezequiel.garcia@free-electrons.com>");
  601. MODULE_DESCRIPTION("Marvell EBU Armada SoCs thermal driver");
  602. MODULE_LICENSE("GPL v2");