armada_thermal.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Marvell EBU Armada SoCs thermal sensor driver
  4. *
  5. * Copyright (C) 2013 Marvell
  6. */
  7. #include <linux/device.h>
  8. #include <linux/err.h>
  9. #include <linux/io.h>
  10. #include <linux/kernel.h>
  11. #include <linux/of.h>
  12. #include <linux/module.h>
  13. #include <linux/delay.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/of_device.h>
  16. #include <linux/thermal.h>
  17. #include <linux/iopoll.h>
  18. #include <linux/mfd/syscon.h>
  19. #include <linux/regmap.h>
  20. #include <linux/interrupt.h>
  21. /* Thermal Manager Control and Status Register */
  22. #define PMU_TDC0_SW_RST_MASK (0x1 << 1)
  23. #define PMU_TM_DISABLE_OFFS 0
  24. #define PMU_TM_DISABLE_MASK (0x1 << PMU_TM_DISABLE_OFFS)
  25. #define PMU_TDC0_REF_CAL_CNT_OFFS 11
  26. #define PMU_TDC0_REF_CAL_CNT_MASK (0x1ff << PMU_TDC0_REF_CAL_CNT_OFFS)
  27. #define PMU_TDC0_OTF_CAL_MASK (0x1 << 30)
  28. #define PMU_TDC0_START_CAL_MASK (0x1 << 25)
  29. #define A375_UNIT_CONTROL_SHIFT 27
  30. #define A375_UNIT_CONTROL_MASK 0x7
  31. #define A375_READOUT_INVERT BIT(15)
  32. #define A375_HW_RESETn BIT(8)
  33. /* Errata fields */
  34. #define CONTROL0_TSEN_TC_TRIM_MASK 0x7
  35. #define CONTROL0_TSEN_TC_TRIM_VAL 0x3
  36. #define CONTROL0_TSEN_START BIT(0)
  37. #define CONTROL0_TSEN_RESET BIT(1)
  38. #define CONTROL0_TSEN_ENABLE BIT(2)
  39. #define CONTROL0_TSEN_AVG_BYPASS BIT(6)
  40. #define CONTROL0_TSEN_CHAN_SHIFT 13
  41. #define CONTROL0_TSEN_CHAN_MASK 0xF
  42. #define CONTROL0_TSEN_OSR_SHIFT 24
  43. #define CONTROL0_TSEN_OSR_MAX 0x3
  44. #define CONTROL0_TSEN_MODE_SHIFT 30
  45. #define CONTROL0_TSEN_MODE_EXTERNAL 0x2
  46. #define CONTROL0_TSEN_MODE_MASK 0x3
  47. #define CONTROL1_TSEN_AVG_MASK 0x7
  48. #define CONTROL1_EXT_TSEN_SW_RESET BIT(7)
  49. #define CONTROL1_EXT_TSEN_HW_RESETn BIT(8)
  50. #define CONTROL1_TSEN_INT_EN BIT(25)
  51. #define CONTROL1_TSEN_SELECT_OFF 21
  52. #define CONTROL1_TSEN_SELECT_MASK 0x3
  53. #define STATUS_POLL_PERIOD_US 1000
  54. #define STATUS_POLL_TIMEOUT_US 100000
  55. #define OVERHEAT_INT_POLL_DELAY_MS 1000
  56. struct armada_thermal_data;
  57. /* Marvell EBU Thermal Sensor Dev Structure */
  58. struct armada_thermal_priv {
  59. struct device *dev;
  60. struct regmap *syscon;
  61. char zone_name[THERMAL_NAME_LENGTH];
  62. /* serialize temperature reads/updates */
  63. struct mutex update_lock;
  64. struct armada_thermal_data *data;
  65. struct thermal_zone_device *overheat_sensor;
  66. int interrupt_source;
  67. int current_channel;
  68. long current_threshold;
  69. long current_hysteresis;
  70. };
  71. struct armada_thermal_data {
  72. /* Initialize the thermal IC */
  73. void (*init)(struct platform_device *pdev,
  74. struct armada_thermal_priv *priv);
  75. /* Formula coeficients: temp = (b - m * reg) / div */
  76. s64 coef_b;
  77. s64 coef_m;
  78. u32 coef_div;
  79. bool inverted;
  80. bool signed_sample;
  81. /* Register shift and mask to access the sensor temperature */
  82. unsigned int temp_shift;
  83. unsigned int temp_mask;
  84. unsigned int thresh_shift;
  85. unsigned int hyst_shift;
  86. unsigned int hyst_mask;
  87. u32 is_valid_bit;
  88. /* Syscon access */
  89. unsigned int syscon_control0_off;
  90. unsigned int syscon_control1_off;
  91. unsigned int syscon_status_off;
  92. unsigned int dfx_irq_cause_off;
  93. unsigned int dfx_irq_mask_off;
  94. unsigned int dfx_overheat_irq;
  95. unsigned int dfx_server_irq_mask_off;
  96. unsigned int dfx_server_irq_en;
  97. /* One sensor is in the thermal IC, the others are in the CPUs if any */
  98. unsigned int cpu_nr;
  99. };
  100. struct armada_drvdata {
  101. enum drvtype {
  102. LEGACY,
  103. SYSCON
  104. } type;
  105. union {
  106. struct armada_thermal_priv *priv;
  107. struct thermal_zone_device *tz;
  108. } data;
  109. };
  110. /*
  111. * struct armada_thermal_sensor - hold the information of one thermal sensor
  112. * @thermal: pointer to the local private structure
  113. * @tzd: pointer to the thermal zone device
  114. * @id: identifier of the thermal sensor
  115. */
  116. struct armada_thermal_sensor {
  117. struct armada_thermal_priv *priv;
  118. int id;
  119. };
  120. static void armadaxp_init(struct platform_device *pdev,
  121. struct armada_thermal_priv *priv)
  122. {
  123. struct armada_thermal_data *data = priv->data;
  124. u32 reg;
  125. regmap_read(priv->syscon, data->syscon_control1_off, &reg);
  126. reg |= PMU_TDC0_OTF_CAL_MASK;
  127. /* Reference calibration value */
  128. reg &= ~PMU_TDC0_REF_CAL_CNT_MASK;
  129. reg |= (0xf1 << PMU_TDC0_REF_CAL_CNT_OFFS);
  130. /* Reset the sensor */
  131. reg |= PMU_TDC0_SW_RST_MASK;
  132. regmap_write(priv->syscon, data->syscon_control1_off, reg);
  133. reg &= ~PMU_TDC0_SW_RST_MASK;
  134. regmap_write(priv->syscon, data->syscon_control1_off, reg);
  135. /* Enable the sensor */
  136. regmap_read(priv->syscon, data->syscon_status_off, &reg);
  137. reg &= ~PMU_TM_DISABLE_MASK;
  138. regmap_write(priv->syscon, data->syscon_status_off, reg);
  139. }
  140. static void armada370_init(struct platform_device *pdev,
  141. struct armada_thermal_priv *priv)
  142. {
  143. struct armada_thermal_data *data = priv->data;
  144. u32 reg;
  145. regmap_read(priv->syscon, data->syscon_control1_off, &reg);
  146. reg |= PMU_TDC0_OTF_CAL_MASK;
  147. /* Reference calibration value */
  148. reg &= ~PMU_TDC0_REF_CAL_CNT_MASK;
  149. reg |= (0xf1 << PMU_TDC0_REF_CAL_CNT_OFFS);
  150. /* Reset the sensor */
  151. reg &= ~PMU_TDC0_START_CAL_MASK;
  152. regmap_write(priv->syscon, data->syscon_control1_off, reg);
  153. msleep(10);
  154. }
  155. static void armada375_init(struct platform_device *pdev,
  156. struct armada_thermal_priv *priv)
  157. {
  158. struct armada_thermal_data *data = priv->data;
  159. u32 reg;
  160. regmap_read(priv->syscon, data->syscon_control1_off, &reg);
  161. reg &= ~(A375_UNIT_CONTROL_MASK << A375_UNIT_CONTROL_SHIFT);
  162. reg &= ~A375_READOUT_INVERT;
  163. reg &= ~A375_HW_RESETn;
  164. regmap_write(priv->syscon, data->syscon_control1_off, reg);
  165. msleep(20);
  166. reg |= A375_HW_RESETn;
  167. regmap_write(priv->syscon, data->syscon_control1_off, reg);
  168. msleep(50);
  169. }
  170. static int armada_wait_sensor_validity(struct armada_thermal_priv *priv)
  171. {
  172. u32 reg;
  173. return regmap_read_poll_timeout(priv->syscon,
  174. priv->data->syscon_status_off, reg,
  175. reg & priv->data->is_valid_bit,
  176. STATUS_POLL_PERIOD_US,
  177. STATUS_POLL_TIMEOUT_US);
  178. }
  179. static void armada380_init(struct platform_device *pdev,
  180. struct armada_thermal_priv *priv)
  181. {
  182. struct armada_thermal_data *data = priv->data;
  183. u32 reg;
  184. /* Disable the HW/SW reset */
  185. regmap_read(priv->syscon, data->syscon_control1_off, &reg);
  186. reg |= CONTROL1_EXT_TSEN_HW_RESETn;
  187. reg &= ~CONTROL1_EXT_TSEN_SW_RESET;
  188. regmap_write(priv->syscon, data->syscon_control1_off, reg);
  189. /* Set Tsen Tc Trim to correct default value (errata #132698) */
  190. regmap_read(priv->syscon, data->syscon_control0_off, &reg);
  191. reg &= ~CONTROL0_TSEN_TC_TRIM_MASK;
  192. reg |= CONTROL0_TSEN_TC_TRIM_VAL;
  193. regmap_write(priv->syscon, data->syscon_control0_off, reg);
  194. }
  195. static void armada_ap80x_init(struct platform_device *pdev,
  196. struct armada_thermal_priv *priv)
  197. {
  198. struct armada_thermal_data *data = priv->data;
  199. u32 reg;
  200. regmap_read(priv->syscon, data->syscon_control0_off, &reg);
  201. reg &= ~CONTROL0_TSEN_RESET;
  202. reg |= CONTROL0_TSEN_START | CONTROL0_TSEN_ENABLE;
  203. /* Sample every ~2ms */
  204. reg |= CONTROL0_TSEN_OSR_MAX << CONTROL0_TSEN_OSR_SHIFT;
  205. /* Enable average (2 samples by default) */
  206. reg &= ~CONTROL0_TSEN_AVG_BYPASS;
  207. regmap_write(priv->syscon, data->syscon_control0_off, reg);
  208. }
  209. static void armada_cp110_init(struct platform_device *pdev,
  210. struct armada_thermal_priv *priv)
  211. {
  212. struct armada_thermal_data *data = priv->data;
  213. u32 reg;
  214. armada380_init(pdev, priv);
  215. /* Sample every ~2ms */
  216. regmap_read(priv->syscon, data->syscon_control0_off, &reg);
  217. reg |= CONTROL0_TSEN_OSR_MAX << CONTROL0_TSEN_OSR_SHIFT;
  218. regmap_write(priv->syscon, data->syscon_control0_off, reg);
  219. /* Average the output value over 2^1 = 2 samples */
  220. regmap_read(priv->syscon, data->syscon_control1_off, &reg);
  221. reg &= ~CONTROL1_TSEN_AVG_MASK;
  222. reg |= 1;
  223. regmap_write(priv->syscon, data->syscon_control1_off, reg);
  224. }
  225. static bool armada_is_valid(struct armada_thermal_priv *priv)
  226. {
  227. u32 reg;
  228. if (!priv->data->is_valid_bit)
  229. return true;
  230. regmap_read(priv->syscon, priv->data->syscon_status_off, &reg);
  231. return reg & priv->data->is_valid_bit;
  232. }
  233. static void armada_enable_overheat_interrupt(struct armada_thermal_priv *priv)
  234. {
  235. struct armada_thermal_data *data = priv->data;
  236. u32 reg;
  237. /* Clear DFX temperature IRQ cause */
  238. regmap_read(priv->syscon, data->dfx_irq_cause_off, &reg);
  239. /* Enable DFX Temperature IRQ */
  240. regmap_read(priv->syscon, data->dfx_irq_mask_off, &reg);
  241. reg |= data->dfx_overheat_irq;
  242. regmap_write(priv->syscon, data->dfx_irq_mask_off, reg);
  243. /* Enable DFX server IRQ */
  244. regmap_read(priv->syscon, data->dfx_server_irq_mask_off, &reg);
  245. reg |= data->dfx_server_irq_en;
  246. regmap_write(priv->syscon, data->dfx_server_irq_mask_off, reg);
  247. /* Enable overheat interrupt */
  248. regmap_read(priv->syscon, data->syscon_control1_off, &reg);
  249. reg |= CONTROL1_TSEN_INT_EN;
  250. regmap_write(priv->syscon, data->syscon_control1_off, reg);
  251. }
  252. static void __maybe_unused
  253. armada_disable_overheat_interrupt(struct armada_thermal_priv *priv)
  254. {
  255. struct armada_thermal_data *data = priv->data;
  256. u32 reg;
  257. regmap_read(priv->syscon, data->syscon_control1_off, &reg);
  258. reg &= ~CONTROL1_TSEN_INT_EN;
  259. regmap_write(priv->syscon, data->syscon_control1_off, reg);
  260. }
  261. /* There is currently no board with more than one sensor per channel */
  262. static int armada_select_channel(struct armada_thermal_priv *priv, int channel)
  263. {
  264. struct armada_thermal_data *data = priv->data;
  265. u32 ctrl0;
  266. if (channel < 0 || channel > priv->data->cpu_nr)
  267. return -EINVAL;
  268. if (priv->current_channel == channel)
  269. return 0;
  270. /* Stop the measurements */
  271. regmap_read(priv->syscon, data->syscon_control0_off, &ctrl0);
  272. ctrl0 &= ~CONTROL0_TSEN_START;
  273. regmap_write(priv->syscon, data->syscon_control0_off, ctrl0);
  274. /* Reset the mode, internal sensor will be automatically selected */
  275. ctrl0 &= ~(CONTROL0_TSEN_MODE_MASK << CONTROL0_TSEN_MODE_SHIFT);
  276. /* Other channels are external and should be selected accordingly */
  277. if (channel) {
  278. /* Change the mode to external */
  279. ctrl0 |= CONTROL0_TSEN_MODE_EXTERNAL <<
  280. CONTROL0_TSEN_MODE_SHIFT;
  281. /* Select the sensor */
  282. ctrl0 &= ~(CONTROL0_TSEN_CHAN_MASK << CONTROL0_TSEN_CHAN_SHIFT);
  283. ctrl0 |= (channel - 1) << CONTROL0_TSEN_CHAN_SHIFT;
  284. }
  285. /* Actually set the mode/channel */
  286. regmap_write(priv->syscon, data->syscon_control0_off, ctrl0);
  287. priv->current_channel = channel;
  288. /* Re-start the measurements */
  289. ctrl0 |= CONTROL0_TSEN_START;
  290. regmap_write(priv->syscon, data->syscon_control0_off, ctrl0);
  291. /*
  292. * The IP has a latency of ~15ms, so after updating the selected source,
  293. * we must absolutely wait for the sensor validity bit to ensure we read
  294. * actual data.
  295. */
  296. if (armada_wait_sensor_validity(priv))
  297. return -EIO;
  298. return 0;
  299. }
  300. static int armada_read_sensor(struct armada_thermal_priv *priv, int *temp)
  301. {
  302. u32 reg, div;
  303. s64 sample, b, m;
  304. regmap_read(priv->syscon, priv->data->syscon_status_off, &reg);
  305. reg = (reg >> priv->data->temp_shift) & priv->data->temp_mask;
  306. if (priv->data->signed_sample)
  307. /* The most significant bit is the sign bit */
  308. sample = sign_extend32(reg, fls(priv->data->temp_mask) - 1);
  309. else
  310. sample = reg;
  311. /* Get formula coeficients */
  312. b = priv->data->coef_b;
  313. m = priv->data->coef_m;
  314. div = priv->data->coef_div;
  315. if (priv->data->inverted)
  316. *temp = div_s64((m * sample) - b, div);
  317. else
  318. *temp = div_s64(b - (m * sample), div);
  319. return 0;
  320. }
  321. static int armada_get_temp_legacy(struct thermal_zone_device *thermal,
  322. int *temp)
  323. {
  324. struct armada_thermal_priv *priv = thermal_zone_device_priv(thermal);
  325. int ret;
  326. /* Valid check */
  327. if (!armada_is_valid(priv))
  328. return -EIO;
  329. /* Do the actual reading */
  330. ret = armada_read_sensor(priv, temp);
  331. return ret;
  332. }
  333. static const struct thermal_zone_device_ops legacy_ops = {
  334. .get_temp = armada_get_temp_legacy,
  335. };
  336. static int armada_get_temp(struct thermal_zone_device *tz, int *temp)
  337. {
  338. struct armada_thermal_sensor *sensor = thermal_zone_device_priv(tz);
  339. struct armada_thermal_priv *priv = sensor->priv;
  340. int ret;
  341. mutex_lock(&priv->update_lock);
  342. /* Select the desired channel */
  343. ret = armada_select_channel(priv, sensor->id);
  344. if (ret)
  345. goto unlock_mutex;
  346. /* Do the actual reading */
  347. ret = armada_read_sensor(priv, temp);
  348. if (ret)
  349. goto unlock_mutex;
  350. /*
  351. * Select back the interrupt source channel from which a potential
  352. * critical trip point has been set.
  353. */
  354. ret = armada_select_channel(priv, priv->interrupt_source);
  355. unlock_mutex:
  356. mutex_unlock(&priv->update_lock);
  357. return ret;
  358. }
  359. static const struct thermal_zone_device_ops of_ops = {
  360. .get_temp = armada_get_temp,
  361. };
  362. static unsigned int armada_mc_to_reg_temp(struct armada_thermal_data *data,
  363. unsigned int temp_mc)
  364. {
  365. s64 b = data->coef_b;
  366. s64 m = data->coef_m;
  367. s64 div = data->coef_div;
  368. unsigned int sample;
  369. if (data->inverted)
  370. sample = div_s64(((temp_mc * div) + b), m);
  371. else
  372. sample = div_s64((b - (temp_mc * div)), m);
  373. return sample & data->temp_mask;
  374. }
  375. /*
  376. * The documentation states:
  377. * high/low watermark = threshold +/- 0.4761 * 2^(hysteresis + 2)
  378. * which is the mathematical derivation for:
  379. * 0x0 <=> 1.9°C, 0x1 <=> 3.8°C, 0x2 <=> 7.6°C, 0x3 <=> 15.2°C
  380. */
  381. static unsigned int hyst_levels_mc[] = {1900, 3800, 7600, 15200};
  382. static unsigned int armada_mc_to_reg_hyst(struct armada_thermal_data *data,
  383. unsigned int hyst_mc)
  384. {
  385. int i;
  386. /*
  387. * We will always take the smallest possible hysteresis to avoid risking
  388. * the hardware integrity by enlarging the threshold by +8°C in the
  389. * worst case.
  390. */
  391. for (i = ARRAY_SIZE(hyst_levels_mc) - 1; i > 0; i--)
  392. if (hyst_mc >= hyst_levels_mc[i])
  393. break;
  394. return i & data->hyst_mask;
  395. }
  396. static void armada_set_overheat_thresholds(struct armada_thermal_priv *priv,
  397. int thresh_mc, int hyst_mc)
  398. {
  399. struct armada_thermal_data *data = priv->data;
  400. unsigned int threshold = armada_mc_to_reg_temp(data, thresh_mc);
  401. unsigned int hysteresis = armada_mc_to_reg_hyst(data, hyst_mc);
  402. u32 ctrl1;
  403. regmap_read(priv->syscon, data->syscon_control1_off, &ctrl1);
  404. /* Set Threshold */
  405. if (thresh_mc >= 0) {
  406. ctrl1 &= ~(data->temp_mask << data->thresh_shift);
  407. ctrl1 |= threshold << data->thresh_shift;
  408. priv->current_threshold = thresh_mc;
  409. }
  410. /* Set Hysteresis */
  411. if (hyst_mc >= 0) {
  412. ctrl1 &= ~(data->hyst_mask << data->hyst_shift);
  413. ctrl1 |= hysteresis << data->hyst_shift;
  414. priv->current_hysteresis = hyst_mc;
  415. }
  416. regmap_write(priv->syscon, data->syscon_control1_off, ctrl1);
  417. }
  418. static irqreturn_t armada_overheat_isr(int irq, void *blob)
  419. {
  420. /*
  421. * Disable the IRQ and continue in thread context (thermal core
  422. * notification and temperature monitoring).
  423. */
  424. disable_irq_nosync(irq);
  425. return IRQ_WAKE_THREAD;
  426. }
  427. static irqreturn_t armada_overheat_isr_thread(int irq, void *blob)
  428. {
  429. struct armada_thermal_priv *priv = blob;
  430. int low_threshold = priv->current_threshold - priv->current_hysteresis;
  431. int temperature;
  432. u32 dummy;
  433. int ret;
  434. /* Notify the core in thread context */
  435. thermal_zone_device_update(priv->overheat_sensor,
  436. THERMAL_EVENT_UNSPECIFIED);
  437. /*
  438. * The overheat interrupt must be cleared by reading the DFX interrupt
  439. * cause _after_ the temperature has fallen down to the low threshold.
  440. * Otherwise future interrupts might not be served.
  441. */
  442. do {
  443. msleep(OVERHEAT_INT_POLL_DELAY_MS);
  444. mutex_lock(&priv->update_lock);
  445. ret = armada_read_sensor(priv, &temperature);
  446. mutex_unlock(&priv->update_lock);
  447. if (ret)
  448. goto enable_irq;
  449. } while (temperature >= low_threshold);
  450. regmap_read(priv->syscon, priv->data->dfx_irq_cause_off, &dummy);
  451. /* Notify the thermal core that the temperature is acceptable again */
  452. thermal_zone_device_update(priv->overheat_sensor,
  453. THERMAL_EVENT_UNSPECIFIED);
  454. enable_irq:
  455. enable_irq(irq);
  456. return IRQ_HANDLED;
  457. }
  458. static const struct armada_thermal_data armadaxp_data = {
  459. .init = armadaxp_init,
  460. .temp_shift = 10,
  461. .temp_mask = 0x1ff,
  462. .coef_b = 3153000000ULL,
  463. .coef_m = 10000000ULL,
  464. .coef_div = 13825,
  465. .syscon_status_off = 0xb0,
  466. .syscon_control1_off = 0x2d0,
  467. };
  468. static const struct armada_thermal_data armada370_data = {
  469. .init = armada370_init,
  470. .is_valid_bit = BIT(9),
  471. .temp_shift = 10,
  472. .temp_mask = 0x1ff,
  473. .coef_b = 3153000000ULL,
  474. .coef_m = 10000000ULL,
  475. .coef_div = 13825,
  476. .syscon_status_off = 0x0,
  477. .syscon_control1_off = 0x4,
  478. };
  479. static const struct armada_thermal_data armada375_data = {
  480. .init = armada375_init,
  481. .is_valid_bit = BIT(10),
  482. .temp_shift = 0,
  483. .temp_mask = 0x1ff,
  484. .coef_b = 3171900000ULL,
  485. .coef_m = 10000000ULL,
  486. .coef_div = 13616,
  487. .syscon_status_off = 0x78,
  488. .syscon_control0_off = 0x7c,
  489. .syscon_control1_off = 0x80,
  490. };
  491. static const struct armada_thermal_data armada380_data = {
  492. .init = armada380_init,
  493. .is_valid_bit = BIT(10),
  494. .temp_shift = 0,
  495. .temp_mask = 0x3ff,
  496. .coef_b = 1172499100ULL,
  497. .coef_m = 2000096ULL,
  498. .coef_div = 4201,
  499. .inverted = true,
  500. .syscon_control0_off = 0x70,
  501. .syscon_control1_off = 0x74,
  502. .syscon_status_off = 0x78,
  503. };
  504. static const struct armada_thermal_data armada_ap806_data = {
  505. .init = armada_ap80x_init,
  506. .is_valid_bit = BIT(16),
  507. .temp_shift = 0,
  508. .temp_mask = 0x3ff,
  509. .thresh_shift = 3,
  510. .hyst_shift = 19,
  511. .hyst_mask = 0x3,
  512. .coef_b = -150000LL,
  513. .coef_m = 423ULL,
  514. .coef_div = 1,
  515. .inverted = true,
  516. .signed_sample = true,
  517. .syscon_control0_off = 0x84,
  518. .syscon_control1_off = 0x88,
  519. .syscon_status_off = 0x8C,
  520. .dfx_irq_cause_off = 0x108,
  521. .dfx_irq_mask_off = 0x10C,
  522. .dfx_overheat_irq = BIT(22),
  523. .dfx_server_irq_mask_off = 0x104,
  524. .dfx_server_irq_en = BIT(1),
  525. .cpu_nr = 4,
  526. };
  527. static const struct armada_thermal_data armada_ap807_data = {
  528. .init = armada_ap80x_init,
  529. .is_valid_bit = BIT(16),
  530. .temp_shift = 0,
  531. .temp_mask = 0x3ff,
  532. .thresh_shift = 3,
  533. .hyst_shift = 19,
  534. .hyst_mask = 0x3,
  535. .coef_b = -128900LL,
  536. .coef_m = 394ULL,
  537. .coef_div = 1,
  538. .inverted = true,
  539. .signed_sample = true,
  540. .syscon_control0_off = 0x84,
  541. .syscon_control1_off = 0x88,
  542. .syscon_status_off = 0x8C,
  543. .dfx_irq_cause_off = 0x108,
  544. .dfx_irq_mask_off = 0x10C,
  545. .dfx_overheat_irq = BIT(22),
  546. .dfx_server_irq_mask_off = 0x104,
  547. .dfx_server_irq_en = BIT(1),
  548. .cpu_nr = 4,
  549. };
  550. static const struct armada_thermal_data armada_cp110_data = {
  551. .init = armada_cp110_init,
  552. .is_valid_bit = BIT(10),
  553. .temp_shift = 0,
  554. .temp_mask = 0x3ff,
  555. .thresh_shift = 16,
  556. .hyst_shift = 26,
  557. .hyst_mask = 0x3,
  558. .coef_b = 1172499100ULL,
  559. .coef_m = 2000096ULL,
  560. .coef_div = 4201,
  561. .inverted = true,
  562. .syscon_control0_off = 0x70,
  563. .syscon_control1_off = 0x74,
  564. .syscon_status_off = 0x78,
  565. .dfx_irq_cause_off = 0x108,
  566. .dfx_irq_mask_off = 0x10C,
  567. .dfx_overheat_irq = BIT(20),
  568. .dfx_server_irq_mask_off = 0x104,
  569. .dfx_server_irq_en = BIT(1),
  570. };
  571. static const struct of_device_id armada_thermal_id_table[] = {
  572. {
  573. .compatible = "marvell,armadaxp-thermal",
  574. .data = &armadaxp_data,
  575. },
  576. {
  577. .compatible = "marvell,armada370-thermal",
  578. .data = &armada370_data,
  579. },
  580. {
  581. .compatible = "marvell,armada375-thermal",
  582. .data = &armada375_data,
  583. },
  584. {
  585. .compatible = "marvell,armada380-thermal",
  586. .data = &armada380_data,
  587. },
  588. {
  589. .compatible = "marvell,armada-ap806-thermal",
  590. .data = &armada_ap806_data,
  591. },
  592. {
  593. .compatible = "marvell,armada-ap807-thermal",
  594. .data = &armada_ap807_data,
  595. },
  596. {
  597. .compatible = "marvell,armada-cp110-thermal",
  598. .data = &armada_cp110_data,
  599. },
  600. {
  601. /* sentinel */
  602. },
  603. };
  604. MODULE_DEVICE_TABLE(of, armada_thermal_id_table);
  605. static const struct regmap_config armada_thermal_regmap_config = {
  606. .reg_bits = 32,
  607. .reg_stride = 4,
  608. .val_bits = 32,
  609. .fast_io = true,
  610. };
  611. static int armada_thermal_probe_legacy(struct platform_device *pdev,
  612. struct armada_thermal_priv *priv)
  613. {
  614. struct armada_thermal_data *data = priv->data;
  615. void __iomem *base;
  616. /* First memory region points towards the status register */
  617. base = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
  618. if (IS_ERR(base))
  619. return PTR_ERR(base);
  620. /*
  621. * Fix up from the old individual DT register specification to
  622. * cover all the registers. We do this by adjusting the ioremap()
  623. * result, which should be fine as ioremap() deals with pages.
  624. * However, validate that we do not cross a page boundary while
  625. * making this adjustment.
  626. */
  627. if (((unsigned long)base & ~PAGE_MASK) < data->syscon_status_off)
  628. return -EINVAL;
  629. base -= data->syscon_status_off;
  630. priv->syscon = devm_regmap_init_mmio(&pdev->dev, base,
  631. &armada_thermal_regmap_config);
  632. return PTR_ERR_OR_ZERO(priv->syscon);
  633. }
  634. static int armada_thermal_probe_syscon(struct platform_device *pdev,
  635. struct armada_thermal_priv *priv)
  636. {
  637. priv->syscon = syscon_node_to_regmap(pdev->dev.parent->of_node);
  638. return PTR_ERR_OR_ZERO(priv->syscon);
  639. }
  640. static void armada_set_sane_name(struct platform_device *pdev,
  641. struct armada_thermal_priv *priv)
  642. {
  643. const char *name = dev_name(&pdev->dev);
  644. if (strlen(name) > THERMAL_NAME_LENGTH) {
  645. /*
  646. * When inside a system controller, the device name has the
  647. * form: f06f8000.system-controller:ap-thermal so stripping
  648. * after the ':' should give us a shorter but meaningful name.
  649. */
  650. name = strrchr(name, ':');
  651. if (!name)
  652. name = "armada_thermal";
  653. else
  654. name++;
  655. }
  656. /* Save the name locally */
  657. strscpy(priv->zone_name, name, THERMAL_NAME_LENGTH);
  658. /* Then ensure there are no '-' or hwmon core will complain */
  659. strreplace(priv->zone_name, '-', '_');
  660. }
  661. /*
  662. * The IP can manage to trigger interrupts on overheat situation from all the
  663. * sensors. However, the interrupt source changes along with the last selected
  664. * source (ie. the last read sensor), which is an inconsistent behavior. Avoid
  665. * possible glitches by always selecting back only one channel (arbitrarily: the
  666. * first in the DT which has a critical trip point). We also disable sensor
  667. * switch during overheat situations.
  668. */
  669. static int armada_configure_overheat_int(struct armada_thermal_priv *priv,
  670. struct thermal_zone_device *tz,
  671. int sensor_id)
  672. {
  673. /* Retrieve the critical trip point to enable the overheat interrupt */
  674. int temperature;
  675. int ret;
  676. ret = thermal_zone_get_crit_temp(tz, &temperature);
  677. if (ret)
  678. return ret;
  679. ret = armada_select_channel(priv, sensor_id);
  680. if (ret)
  681. return ret;
  682. /*
  683. * A critical temperature does not have a hysteresis
  684. */
  685. armada_set_overheat_thresholds(priv, temperature, 0);
  686. priv->overheat_sensor = tz;
  687. priv->interrupt_source = sensor_id;
  688. armada_enable_overheat_interrupt(priv);
  689. return 0;
  690. }
  691. static int armada_thermal_probe(struct platform_device *pdev)
  692. {
  693. struct thermal_zone_device *tz;
  694. struct armada_thermal_sensor *sensor;
  695. struct armada_drvdata *drvdata;
  696. const struct of_device_id *match;
  697. struct armada_thermal_priv *priv;
  698. int sensor_id, irq;
  699. int ret;
  700. match = of_match_device(armada_thermal_id_table, &pdev->dev);
  701. if (!match)
  702. return -ENODEV;
  703. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  704. if (!priv)
  705. return -ENOMEM;
  706. drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
  707. if (!drvdata)
  708. return -ENOMEM;
  709. priv->dev = &pdev->dev;
  710. priv->data = (struct armada_thermal_data *)match->data;
  711. mutex_init(&priv->update_lock);
  712. /*
  713. * Legacy DT bindings only described "control1" register (also referred
  714. * as "control MSB" on old documentation). Then, bindings moved to cover
  715. * "control0/control LSB" and "control1/control MSB" registers within
  716. * the same resource, which was then of size 8 instead of 4.
  717. *
  718. * The logic of defining sporadic registers is broken. For instance, it
  719. * blocked the addition of the overheat interrupt feature that needed
  720. * another resource somewhere else in the same memory area. One solution
  721. * is to define an overall system controller and put the thermal node
  722. * into it, which requires the use of regmaps across all the driver.
  723. */
  724. if (IS_ERR(syscon_node_to_regmap(pdev->dev.parent->of_node))) {
  725. /* Ensure device name is correct for the thermal core */
  726. armada_set_sane_name(pdev, priv);
  727. ret = armada_thermal_probe_legacy(pdev, priv);
  728. if (ret)
  729. return ret;
  730. priv->data->init(pdev, priv);
  731. /* Wait the sensors to be valid */
  732. armada_wait_sensor_validity(priv);
  733. tz = thermal_tripless_zone_device_register(priv->zone_name,
  734. priv, &legacy_ops,
  735. NULL);
  736. if (IS_ERR(tz)) {
  737. dev_err(&pdev->dev,
  738. "Failed to register thermal zone device\n");
  739. return PTR_ERR(tz);
  740. }
  741. ret = thermal_zone_device_enable(tz);
  742. if (ret) {
  743. thermal_zone_device_unregister(tz);
  744. return ret;
  745. }
  746. drvdata->type = LEGACY;
  747. drvdata->data.tz = tz;
  748. platform_set_drvdata(pdev, drvdata);
  749. return 0;
  750. }
  751. ret = armada_thermal_probe_syscon(pdev, priv);
  752. if (ret)
  753. return ret;
  754. priv->current_channel = -1;
  755. priv->data->init(pdev, priv);
  756. drvdata->type = SYSCON;
  757. drvdata->data.priv = priv;
  758. platform_set_drvdata(pdev, drvdata);
  759. irq = platform_get_irq(pdev, 0);
  760. if (irq == -EPROBE_DEFER)
  761. return irq;
  762. /* The overheat interrupt feature is not mandatory */
  763. if (irq > 0) {
  764. ret = devm_request_threaded_irq(&pdev->dev, irq,
  765. armada_overheat_isr,
  766. armada_overheat_isr_thread,
  767. 0, NULL, priv);
  768. if (ret) {
  769. dev_err(&pdev->dev, "Cannot request threaded IRQ %d\n",
  770. irq);
  771. return ret;
  772. }
  773. }
  774. /*
  775. * There is one channel for the IC and one per CPU (if any), each
  776. * channel has one sensor.
  777. */
  778. for (sensor_id = 0; sensor_id <= priv->data->cpu_nr; sensor_id++) {
  779. sensor = devm_kzalloc(&pdev->dev,
  780. sizeof(struct armada_thermal_sensor),
  781. GFP_KERNEL);
  782. if (!sensor)
  783. return -ENOMEM;
  784. /* Register the sensor */
  785. sensor->priv = priv;
  786. sensor->id = sensor_id;
  787. tz = devm_thermal_of_zone_register(&pdev->dev,
  788. sensor->id, sensor,
  789. &of_ops);
  790. if (IS_ERR(tz)) {
  791. dev_info(&pdev->dev, "Thermal sensor %d unavailable\n",
  792. sensor_id);
  793. devm_kfree(&pdev->dev, sensor);
  794. continue;
  795. }
  796. /*
  797. * The first channel that has a critical trip point registered
  798. * in the DT will serve as interrupt source. Others possible
  799. * critical trip points will simply be ignored by the driver.
  800. */
  801. if (irq > 0 && !priv->overheat_sensor)
  802. armada_configure_overheat_int(priv, tz, sensor->id);
  803. }
  804. /* Just complain if no overheat interrupt was set up */
  805. if (!priv->overheat_sensor)
  806. dev_warn(&pdev->dev, "Overheat interrupt not available\n");
  807. return 0;
  808. }
  809. static void armada_thermal_exit(struct platform_device *pdev)
  810. {
  811. struct armada_drvdata *drvdata = platform_get_drvdata(pdev);
  812. if (drvdata->type == LEGACY)
  813. thermal_zone_device_unregister(drvdata->data.tz);
  814. }
  815. static struct platform_driver armada_thermal_driver = {
  816. .probe = armada_thermal_probe,
  817. .remove = armada_thermal_exit,
  818. .driver = {
  819. .name = "armada_thermal",
  820. .of_match_table = armada_thermal_id_table,
  821. },
  822. };
  823. module_platform_driver(armada_thermal_driver);
  824. MODULE_AUTHOR("Ezequiel Garcia <ezequiel.garcia@free-electrons.com>");
  825. MODULE_DESCRIPTION("Marvell EBU Armada SoCs thermal driver");
  826. MODULE_LICENSE("GPL v2");