015-4-thermal-qcom-tsens-8960-Add-support-for-8960-family-of-SoCs.patch 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. From 20d4fd84bf524ad91e2cc3e4ab4020c27cfc0081 Mon Sep 17 00:00:00 2001
  2. From: Rajendra Nayak <rnayak@codeaurora.org>
  3. Date: Thu, 5 May 2016 14:21:43 +0530
  4. Subject: thermal: qcom: tsens-8960: Add support for 8960 family of SoCs
  5. 8960 family of SoCs have the TSENS device as part of GCC, hence
  6. the driver probes the virtual child device created by GCC and
  7. uses the parent to extract all DT properties and reuses the GCC
  8. regmap.
  9. Also GCC/TSENS are part of a domain thats not always ON.
  10. Hence add .suspend and .resume hooks to save and restore some of
  11. the inited register context.
  12. Also 8960 family have some of the TSENS init sequence thats
  13. required to be done by the HLOS driver (some later versions of TSENS
  14. do not export these registers to non-secure world, and hence need
  15. these initializations to be done by secure bootloaders)
  16. 8660 from the same family has just one sensor and hence some register
  17. offset/layout differences which need special handling in the driver.
  18. Based on the original code from Siddartha Mohanadoss, Stephen Boyd and
  19. Narendran Rajan.
  20. Signed-off-by: Rajendra Nayak <rnayak@codeaurora.org>
  21. Signed-off-by: Eduardo Valentin <edubezval@gmail.com>
  22. Signed-off-by: Zhang Rui <rui.zhang@intel.com>
  23. ---
  24. drivers/thermal/qcom/Makefile | 2 +-
  25. drivers/thermal/qcom/tsens-8960.c | 292 ++++++++++++++++++++++++++++++++++++++
  26. drivers/thermal/qcom/tsens.c | 8 +-
  27. drivers/thermal/qcom/tsens.h | 2 +-
  28. 4 files changed, 298 insertions(+), 6 deletions(-)
  29. create mode 100644 drivers/thermal/qcom/tsens-8960.c
  30. --- a/drivers/thermal/qcom/Makefile
  31. +++ b/drivers/thermal/qcom/Makefile
  32. @@ -1,2 +1,2 @@
  33. obj-$(CONFIG_QCOM_TSENS) += qcom_tsens.o
  34. -qcom_tsens-y += tsens.o tsens-common.o tsens-8916.o tsens-8974.o
  35. +qcom_tsens-y += tsens.o tsens-common.o tsens-8916.o tsens-8974.o tsens-8960.o
  36. --- /dev/null
  37. +++ b/drivers/thermal/qcom/tsens-8960.c
  38. @@ -0,0 +1,292 @@
  39. +/*
  40. + * Copyright (c) 2015, The Linux Foundation. All rights reserved.
  41. + *
  42. + * This program is free software; you can redistribute it and/or modify
  43. + * it under the terms of the GNU General Public License version 2 and
  44. + * only version 2 as published by the Free Software Foundation.
  45. + *
  46. + * This program is distributed in the hope that it will be useful,
  47. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  48. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  49. + * GNU General Public License for more details.
  50. + *
  51. + */
  52. +
  53. +#include <linux/platform_device.h>
  54. +#include <linux/delay.h>
  55. +#include <linux/bitops.h>
  56. +#include <linux/regmap.h>
  57. +#include <linux/thermal.h>
  58. +#include "tsens.h"
  59. +
  60. +#define CAL_MDEGC 30000
  61. +
  62. +#define CONFIG_ADDR 0x3640
  63. +#define CONFIG_ADDR_8660 0x3620
  64. +/* CONFIG_ADDR bitmasks */
  65. +#define CONFIG 0x9b
  66. +#define CONFIG_MASK 0xf
  67. +#define CONFIG_8660 1
  68. +#define CONFIG_SHIFT_8660 28
  69. +#define CONFIG_MASK_8660 (3 << CONFIG_SHIFT_8660)
  70. +
  71. +#define STATUS_CNTL_ADDR_8064 0x3660
  72. +#define CNTL_ADDR 0x3620
  73. +/* CNTL_ADDR bitmasks */
  74. +#define EN BIT(0)
  75. +#define SW_RST BIT(1)
  76. +#define SENSOR0_EN BIT(3)
  77. +#define SLP_CLK_ENA BIT(26)
  78. +#define SLP_CLK_ENA_8660 BIT(24)
  79. +#define MEASURE_PERIOD 1
  80. +#define SENSOR0_SHIFT 3
  81. +
  82. +/* INT_STATUS_ADDR bitmasks */
  83. +#define MIN_STATUS_MASK BIT(0)
  84. +#define LOWER_STATUS_CLR BIT(1)
  85. +#define UPPER_STATUS_CLR BIT(2)
  86. +#define MAX_STATUS_MASK BIT(3)
  87. +
  88. +#define THRESHOLD_ADDR 0x3624
  89. +/* THRESHOLD_ADDR bitmasks */
  90. +#define THRESHOLD_MAX_LIMIT_SHIFT 24
  91. +#define THRESHOLD_MIN_LIMIT_SHIFT 16
  92. +#define THRESHOLD_UPPER_LIMIT_SHIFT 8
  93. +#define THRESHOLD_LOWER_LIMIT_SHIFT 0
  94. +
  95. +/* Initial temperature threshold values */
  96. +#define LOWER_LIMIT_TH 0x50
  97. +#define UPPER_LIMIT_TH 0xdf
  98. +#define MIN_LIMIT_TH 0x0
  99. +#define MAX_LIMIT_TH 0xff
  100. +
  101. +#define S0_STATUS_ADDR 0x3628
  102. +#define INT_STATUS_ADDR 0x363c
  103. +#define TRDY_MASK BIT(7)
  104. +#define TIMEOUT_US 100
  105. +
  106. +static int suspend_8960(struct tsens_device *tmdev)
  107. +{
  108. + int ret;
  109. + unsigned int mask;
  110. + struct regmap *map = tmdev->map;
  111. +
  112. + ret = regmap_read(map, THRESHOLD_ADDR, &tmdev->ctx.threshold);
  113. + if (ret)
  114. + return ret;
  115. +
  116. + ret = regmap_read(map, CNTL_ADDR, &tmdev->ctx.control);
  117. + if (ret)
  118. + return ret;
  119. +
  120. + if (tmdev->num_sensors > 1)
  121. + mask = SLP_CLK_ENA | EN;
  122. + else
  123. + mask = SLP_CLK_ENA_8660 | EN;
  124. +
  125. + ret = regmap_update_bits(map, CNTL_ADDR, mask, 0);
  126. + if (ret)
  127. + return ret;
  128. +
  129. + return 0;
  130. +}
  131. +
  132. +static int resume_8960(struct tsens_device *tmdev)
  133. +{
  134. + int ret;
  135. + struct regmap *map = tmdev->map;
  136. +
  137. + ret = regmap_update_bits(map, CNTL_ADDR, SW_RST, SW_RST);
  138. + if (ret)
  139. + return ret;
  140. +
  141. + /*
  142. + * Separate CONFIG restore is not needed only for 8660 as
  143. + * config is part of CTRL Addr and its restored as such
  144. + */
  145. + if (tmdev->num_sensors > 1) {
  146. + ret = regmap_update_bits(map, CONFIG_ADDR, CONFIG_MASK, CONFIG);
  147. + if (ret)
  148. + return ret;
  149. + }
  150. +
  151. + ret = regmap_write(map, THRESHOLD_ADDR, tmdev->ctx.threshold);
  152. + if (ret)
  153. + return ret;
  154. +
  155. + ret = regmap_write(map, CNTL_ADDR, tmdev->ctx.control);
  156. + if (ret)
  157. + return ret;
  158. +
  159. + return 0;
  160. +}
  161. +
  162. +static int enable_8960(struct tsens_device *tmdev, int id)
  163. +{
  164. + int ret;
  165. + u32 reg, mask;
  166. +
  167. + ret = regmap_read(tmdev->map, CNTL_ADDR, &reg);
  168. + if (ret)
  169. + return ret;
  170. +
  171. + mask = BIT(id + SENSOR0_SHIFT);
  172. + ret = regmap_write(tmdev->map, CNTL_ADDR, reg | SW_RST);
  173. + if (ret)
  174. + return ret;
  175. +
  176. + if (tmdev->num_sensors > 1)
  177. + reg |= mask | SLP_CLK_ENA | EN;
  178. + else
  179. + reg |= mask | SLP_CLK_ENA_8660 | EN;
  180. +
  181. + ret = regmap_write(tmdev->map, CNTL_ADDR, reg);
  182. + if (ret)
  183. + return ret;
  184. +
  185. + return 0;
  186. +}
  187. +
  188. +static void disable_8960(struct tsens_device *tmdev)
  189. +{
  190. + int ret;
  191. + u32 reg_cntl;
  192. + u32 mask;
  193. +
  194. + mask = GENMASK(tmdev->num_sensors - 1, 0);
  195. + mask <<= SENSOR0_SHIFT;
  196. + mask |= EN;
  197. +
  198. + ret = regmap_read(tmdev->map, CNTL_ADDR, &reg_cntl);
  199. + if (ret)
  200. + return;
  201. +
  202. + reg_cntl &= ~mask;
  203. +
  204. + if (tmdev->num_sensors > 1)
  205. + reg_cntl &= ~SLP_CLK_ENA;
  206. + else
  207. + reg_cntl &= ~SLP_CLK_ENA_8660;
  208. +
  209. + regmap_write(tmdev->map, CNTL_ADDR, reg_cntl);
  210. +}
  211. +
  212. +static int init_8960(struct tsens_device *tmdev)
  213. +{
  214. + int ret, i;
  215. + u32 reg_cntl;
  216. +
  217. + tmdev->map = dev_get_regmap(tmdev->dev, NULL);
  218. + if (!tmdev->map)
  219. + return -ENODEV;
  220. +
  221. + /*
  222. + * The status registers for each sensor are discontiguous
  223. + * because some SoCs have 5 sensors while others have more
  224. + * but the control registers stay in the same place, i.e
  225. + * directly after the first 5 status registers.
  226. + */
  227. + for (i = 0; i < tmdev->num_sensors; i++) {
  228. + if (i >= 5)
  229. + tmdev->sensor[i].status = S0_STATUS_ADDR + 40;
  230. + tmdev->sensor[i].status += i * 4;
  231. + }
  232. +
  233. + reg_cntl = SW_RST;
  234. + ret = regmap_update_bits(tmdev->map, CNTL_ADDR, SW_RST, reg_cntl);
  235. + if (ret)
  236. + return ret;
  237. +
  238. + if (tmdev->num_sensors > 1) {
  239. + reg_cntl |= SLP_CLK_ENA | (MEASURE_PERIOD << 18);
  240. + reg_cntl &= ~SW_RST;
  241. + ret = regmap_update_bits(tmdev->map, CONFIG_ADDR,
  242. + CONFIG_MASK, CONFIG);
  243. + } else {
  244. + reg_cntl |= SLP_CLK_ENA_8660 | (MEASURE_PERIOD << 16);
  245. + reg_cntl &= ~CONFIG_MASK_8660;
  246. + reg_cntl |= CONFIG_8660 << CONFIG_SHIFT_8660;
  247. + }
  248. +
  249. + reg_cntl |= GENMASK(tmdev->num_sensors - 1, 0) << SENSOR0_SHIFT;
  250. + ret = regmap_write(tmdev->map, CNTL_ADDR, reg_cntl);
  251. + if (ret)
  252. + return ret;
  253. +
  254. + reg_cntl |= EN;
  255. + ret = regmap_write(tmdev->map, CNTL_ADDR, reg_cntl);
  256. + if (ret)
  257. + return ret;
  258. +
  259. + return 0;
  260. +}
  261. +
  262. +static int calibrate_8960(struct tsens_device *tmdev)
  263. +{
  264. + int i;
  265. + char *data;
  266. +
  267. + ssize_t num_read = tmdev->num_sensors;
  268. + struct tsens_sensor *s = tmdev->sensor;
  269. +
  270. + data = qfprom_read(tmdev->dev, "calib");
  271. + if (IS_ERR(data))
  272. + data = qfprom_read(tmdev->dev, "calib_backup");
  273. + if (IS_ERR(data))
  274. + return PTR_ERR(data);
  275. +
  276. + for (i = 0; i < num_read; i++, s++)
  277. + s->offset = data[i];
  278. +
  279. + return 0;
  280. +}
  281. +
  282. +/* Temperature on y axis and ADC-code on x-axis */
  283. +static inline int code_to_mdegC(u32 adc_code, const struct tsens_sensor *s)
  284. +{
  285. + int slope, offset;
  286. +
  287. + slope = thermal_zone_get_slope(s->tzd);
  288. + offset = CAL_MDEGC - slope * s->offset;
  289. +
  290. + return adc_code * slope + offset;
  291. +}
  292. +
  293. +static int get_temp_8960(struct tsens_device *tmdev, int id, int *temp)
  294. +{
  295. + int ret;
  296. + u32 code, trdy;
  297. + const struct tsens_sensor *s = &tmdev->sensor[id];
  298. + unsigned long timeout;
  299. +
  300. + timeout = jiffies + usecs_to_jiffies(TIMEOUT_US);
  301. + do {
  302. + ret = regmap_read(tmdev->map, INT_STATUS_ADDR, &trdy);
  303. + if (ret)
  304. + return ret;
  305. + if (!(trdy & TRDY_MASK))
  306. + continue;
  307. + ret = regmap_read(tmdev->map, s->status, &code);
  308. + if (ret)
  309. + return ret;
  310. + *temp = code_to_mdegC(code, s);
  311. + return 0;
  312. + } while (time_before(jiffies, timeout));
  313. +
  314. + return -ETIMEDOUT;
  315. +}
  316. +
  317. +const struct tsens_ops ops_8960 = {
  318. + .init = init_8960,
  319. + .calibrate = calibrate_8960,
  320. + .get_temp = get_temp_8960,
  321. + .enable = enable_8960,
  322. + .disable = disable_8960,
  323. + .suspend = suspend_8960,
  324. + .resume = resume_8960,
  325. +};
  326. +
  327. +const struct tsens_data data_8960 = {
  328. + .num_sensors = 11,
  329. + .ops = &ops_8960,
  330. +};
  331. --- a/drivers/thermal/qcom/tsens.c
  332. +++ b/drivers/thermal/qcom/tsens.c
  333. @@ -122,10 +122,10 @@ static int tsens_probe(struct platform_d
  334. np = dev->of_node;
  335. id = of_match_node(tsens_table, np);
  336. - if (!id)
  337. - return -EINVAL;
  338. -
  339. - data = id->data;
  340. + if (id)
  341. + data = id->data;
  342. + else
  343. + data = &data_8960;
  344. if (data->num_sensors <= 0) {
  345. dev_err(dev, "invalid number of sensors\n");
  346. --- a/drivers/thermal/qcom/tsens.h
  347. +++ b/drivers/thermal/qcom/tsens.h
  348. @@ -87,6 +87,6 @@ void compute_intercept_slope(struct tsen
  349. int init_common(struct tsens_device *);
  350. int get_temp_common(struct tsens_device *, int, int *);
  351. -extern const struct tsens_data data_8916, data_8974;
  352. +extern const struct tsens_data data_8916, data_8974, data_8960;
  353. #endif /* __QCOM_TSENS_H__ */