pm8941-wled.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /* Copyright (c) 2015, Sony Mobile Communications, AB.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/backlight.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/of_device.h>
  17. #include <linux/regmap.h>
  18. /* From DT binding */
  19. #define PM8941_WLED_DEFAULT_BRIGHTNESS 2048
  20. #define PM8941_WLED_REG_VAL_BASE 0x40
  21. #define PM8941_WLED_REG_VAL_MAX 0xFFF
  22. #define PM8941_WLED_REG_MOD_EN 0x46
  23. #define PM8941_WLED_REG_MOD_EN_BIT BIT(7)
  24. #define PM8941_WLED_REG_MOD_EN_MASK BIT(7)
  25. #define PM8941_WLED_REG_SYNC 0x47
  26. #define PM8941_WLED_REG_SYNC_MASK 0x07
  27. #define PM8941_WLED_REG_SYNC_LED1 BIT(0)
  28. #define PM8941_WLED_REG_SYNC_LED2 BIT(1)
  29. #define PM8941_WLED_REG_SYNC_LED3 BIT(2)
  30. #define PM8941_WLED_REG_SYNC_ALL 0x07
  31. #define PM8941_WLED_REG_SYNC_CLEAR 0x00
  32. #define PM8941_WLED_REG_FREQ 0x4c
  33. #define PM8941_WLED_REG_FREQ_MASK 0x0f
  34. #define PM8941_WLED_REG_OVP 0x4d
  35. #define PM8941_WLED_REG_OVP_MASK 0x03
  36. #define PM8941_WLED_REG_BOOST 0x4e
  37. #define PM8941_WLED_REG_BOOST_MASK 0x07
  38. #define PM8941_WLED_REG_SINK 0x4f
  39. #define PM8941_WLED_REG_SINK_MASK 0xe0
  40. #define PM8941_WLED_REG_SINK_SHFT 0x05
  41. /* Per-'string' registers below */
  42. #define PM8941_WLED_REG_STR_OFFSET 0x10
  43. #define PM8941_WLED_REG_STR_MOD_EN_BASE 0x60
  44. #define PM8941_WLED_REG_STR_MOD_MASK BIT(7)
  45. #define PM8941_WLED_REG_STR_MOD_EN BIT(7)
  46. #define PM8941_WLED_REG_STR_SCALE_BASE 0x62
  47. #define PM8941_WLED_REG_STR_SCALE_MASK 0x1f
  48. #define PM8941_WLED_REG_STR_MOD_SRC_BASE 0x63
  49. #define PM8941_WLED_REG_STR_MOD_SRC_MASK 0x01
  50. #define PM8941_WLED_REG_STR_MOD_SRC_INT 0x00
  51. #define PM8941_WLED_REG_STR_MOD_SRC_EXT 0x01
  52. #define PM8941_WLED_REG_STR_CABC_BASE 0x66
  53. #define PM8941_WLED_REG_STR_CABC_MASK BIT(7)
  54. #define PM8941_WLED_REG_STR_CABC_EN BIT(7)
  55. struct pm8941_wled_config {
  56. u32 i_boost_limit;
  57. u32 ovp;
  58. u32 switch_freq;
  59. u32 num_strings;
  60. u32 i_limit;
  61. bool cs_out_en;
  62. bool ext_gen;
  63. bool cabc_en;
  64. };
  65. struct pm8941_wled {
  66. const char *name;
  67. struct regmap *regmap;
  68. u16 addr;
  69. struct pm8941_wled_config cfg;
  70. };
  71. static int pm8941_wled_update_status(struct backlight_device *bl)
  72. {
  73. struct pm8941_wled *wled = bl_get_data(bl);
  74. u16 val = bl->props.brightness;
  75. u8 ctrl = 0;
  76. int rc;
  77. int i;
  78. if (bl->props.power != FB_BLANK_UNBLANK ||
  79. bl->props.fb_blank != FB_BLANK_UNBLANK ||
  80. bl->props.state & BL_CORE_FBBLANK)
  81. val = 0;
  82. if (val != 0)
  83. ctrl = PM8941_WLED_REG_MOD_EN_BIT;
  84. rc = regmap_update_bits(wled->regmap,
  85. wled->addr + PM8941_WLED_REG_MOD_EN,
  86. PM8941_WLED_REG_MOD_EN_MASK, ctrl);
  87. if (rc)
  88. return rc;
  89. for (i = 0; i < wled->cfg.num_strings; ++i) {
  90. u8 v[2] = { val & 0xff, (val >> 8) & 0xf };
  91. rc = regmap_bulk_write(wled->regmap,
  92. wled->addr + PM8941_WLED_REG_VAL_BASE + 2 * i,
  93. v, 2);
  94. if (rc)
  95. return rc;
  96. }
  97. rc = regmap_update_bits(wled->regmap,
  98. wled->addr + PM8941_WLED_REG_SYNC,
  99. PM8941_WLED_REG_SYNC_MASK, PM8941_WLED_REG_SYNC_ALL);
  100. if (rc)
  101. return rc;
  102. rc = regmap_update_bits(wled->regmap,
  103. wled->addr + PM8941_WLED_REG_SYNC,
  104. PM8941_WLED_REG_SYNC_MASK, PM8941_WLED_REG_SYNC_CLEAR);
  105. return rc;
  106. }
  107. static int pm8941_wled_setup(struct pm8941_wled *wled)
  108. {
  109. int rc;
  110. int i;
  111. rc = regmap_update_bits(wled->regmap,
  112. wled->addr + PM8941_WLED_REG_OVP,
  113. PM8941_WLED_REG_OVP_MASK, wled->cfg.ovp);
  114. if (rc)
  115. return rc;
  116. rc = regmap_update_bits(wled->regmap,
  117. wled->addr + PM8941_WLED_REG_BOOST,
  118. PM8941_WLED_REG_BOOST_MASK, wled->cfg.i_boost_limit);
  119. if (rc)
  120. return rc;
  121. rc = regmap_update_bits(wled->regmap,
  122. wled->addr + PM8941_WLED_REG_FREQ,
  123. PM8941_WLED_REG_FREQ_MASK, wled->cfg.switch_freq);
  124. if (rc)
  125. return rc;
  126. if (wled->cfg.cs_out_en) {
  127. u8 all = (BIT(wled->cfg.num_strings) - 1)
  128. << PM8941_WLED_REG_SINK_SHFT;
  129. rc = regmap_update_bits(wled->regmap,
  130. wled->addr + PM8941_WLED_REG_SINK,
  131. PM8941_WLED_REG_SINK_MASK, all);
  132. if (rc)
  133. return rc;
  134. }
  135. for (i = 0; i < wled->cfg.num_strings; ++i) {
  136. u16 addr = wled->addr + PM8941_WLED_REG_STR_OFFSET * i;
  137. rc = regmap_update_bits(wled->regmap,
  138. addr + PM8941_WLED_REG_STR_MOD_EN_BASE,
  139. PM8941_WLED_REG_STR_MOD_MASK,
  140. PM8941_WLED_REG_STR_MOD_EN);
  141. if (rc)
  142. return rc;
  143. if (wled->cfg.ext_gen) {
  144. rc = regmap_update_bits(wled->regmap,
  145. addr + PM8941_WLED_REG_STR_MOD_SRC_BASE,
  146. PM8941_WLED_REG_STR_MOD_SRC_MASK,
  147. PM8941_WLED_REG_STR_MOD_SRC_EXT);
  148. if (rc)
  149. return rc;
  150. }
  151. rc = regmap_update_bits(wled->regmap,
  152. addr + PM8941_WLED_REG_STR_SCALE_BASE,
  153. PM8941_WLED_REG_STR_SCALE_MASK,
  154. wled->cfg.i_limit);
  155. if (rc)
  156. return rc;
  157. rc = regmap_update_bits(wled->regmap,
  158. addr + PM8941_WLED_REG_STR_CABC_BASE,
  159. PM8941_WLED_REG_STR_CABC_MASK,
  160. wled->cfg.cabc_en ?
  161. PM8941_WLED_REG_STR_CABC_EN : 0);
  162. if (rc)
  163. return rc;
  164. }
  165. return 0;
  166. }
  167. static const struct pm8941_wled_config pm8941_wled_config_defaults = {
  168. .i_boost_limit = 3,
  169. .i_limit = 20,
  170. .ovp = 2,
  171. .switch_freq = 5,
  172. .num_strings = 0,
  173. .cs_out_en = false,
  174. .ext_gen = false,
  175. .cabc_en = false,
  176. };
  177. struct pm8941_wled_var_cfg {
  178. const u32 *values;
  179. u32 (*fn)(u32);
  180. int size;
  181. };
  182. static const u32 pm8941_wled_i_boost_limit_values[] = {
  183. 105, 385, 525, 805, 980, 1260, 1400, 1680,
  184. };
  185. static const struct pm8941_wled_var_cfg pm8941_wled_i_boost_limit_cfg = {
  186. .values = pm8941_wled_i_boost_limit_values,
  187. .size = ARRAY_SIZE(pm8941_wled_i_boost_limit_values),
  188. };
  189. static const u32 pm8941_wled_ovp_values[] = {
  190. 35, 32, 29, 27,
  191. };
  192. static const struct pm8941_wled_var_cfg pm8941_wled_ovp_cfg = {
  193. .values = pm8941_wled_ovp_values,
  194. .size = ARRAY_SIZE(pm8941_wled_ovp_values),
  195. };
  196. static u32 pm8941_wled_num_strings_values_fn(u32 idx)
  197. {
  198. return idx + 1;
  199. }
  200. static const struct pm8941_wled_var_cfg pm8941_wled_num_strings_cfg = {
  201. .fn = pm8941_wled_num_strings_values_fn,
  202. .size = 3,
  203. };
  204. static u32 pm8941_wled_switch_freq_values_fn(u32 idx)
  205. {
  206. return 19200 / (2 * (1 + idx));
  207. }
  208. static const struct pm8941_wled_var_cfg pm8941_wled_switch_freq_cfg = {
  209. .fn = pm8941_wled_switch_freq_values_fn,
  210. .size = 16,
  211. };
  212. static const struct pm8941_wled_var_cfg pm8941_wled_i_limit_cfg = {
  213. .size = 26,
  214. };
  215. static u32 pm8941_wled_values(const struct pm8941_wled_var_cfg *cfg, u32 idx)
  216. {
  217. if (idx >= cfg->size)
  218. return UINT_MAX;
  219. if (cfg->fn)
  220. return cfg->fn(idx);
  221. if (cfg->values)
  222. return cfg->values[idx];
  223. return idx;
  224. }
  225. static int pm8941_wled_configure(struct pm8941_wled *wled, struct device *dev)
  226. {
  227. struct pm8941_wled_config *cfg = &wled->cfg;
  228. u32 val;
  229. int rc;
  230. u32 c;
  231. int i;
  232. int j;
  233. const struct {
  234. const char *name;
  235. u32 *val_ptr;
  236. const struct pm8941_wled_var_cfg *cfg;
  237. } u32_opts[] = {
  238. {
  239. "qcom,current-boost-limit",
  240. &cfg->i_boost_limit,
  241. .cfg = &pm8941_wled_i_boost_limit_cfg,
  242. },
  243. {
  244. "qcom,current-limit",
  245. &cfg->i_limit,
  246. .cfg = &pm8941_wled_i_limit_cfg,
  247. },
  248. {
  249. "qcom,ovp",
  250. &cfg->ovp,
  251. .cfg = &pm8941_wled_ovp_cfg,
  252. },
  253. {
  254. "qcom,switching-freq",
  255. &cfg->switch_freq,
  256. .cfg = &pm8941_wled_switch_freq_cfg,
  257. },
  258. {
  259. "qcom,num-strings",
  260. &cfg->num_strings,
  261. .cfg = &pm8941_wled_num_strings_cfg,
  262. },
  263. };
  264. const struct {
  265. const char *name;
  266. bool *val_ptr;
  267. } bool_opts[] = {
  268. { "qcom,cs-out", &cfg->cs_out_en, },
  269. { "qcom,ext-gen", &cfg->ext_gen, },
  270. { "qcom,cabc", &cfg->cabc_en, },
  271. };
  272. rc = of_property_read_u32(dev->of_node, "reg", &val);
  273. if (rc || val > 0xffff) {
  274. dev_err(dev, "invalid IO resources\n");
  275. return rc ? rc : -EINVAL;
  276. }
  277. wled->addr = val;
  278. rc = of_property_read_string(dev->of_node, "label", &wled->name);
  279. if (rc)
  280. wled->name = dev->of_node->name;
  281. *cfg = pm8941_wled_config_defaults;
  282. for (i = 0; i < ARRAY_SIZE(u32_opts); ++i) {
  283. rc = of_property_read_u32(dev->of_node, u32_opts[i].name, &val);
  284. if (rc == -EINVAL) {
  285. continue;
  286. } else if (rc) {
  287. dev_err(dev, "error reading '%s'\n", u32_opts[i].name);
  288. return rc;
  289. }
  290. c = UINT_MAX;
  291. for (j = 0; c != val; j++) {
  292. c = pm8941_wled_values(u32_opts[i].cfg, j);
  293. if (c == UINT_MAX) {
  294. dev_err(dev, "invalid value for '%s'\n",
  295. u32_opts[i].name);
  296. return -EINVAL;
  297. }
  298. }
  299. dev_dbg(dev, "'%s' = %u\n", u32_opts[i].name, c);
  300. *u32_opts[i].val_ptr = j;
  301. }
  302. for (i = 0; i < ARRAY_SIZE(bool_opts); ++i) {
  303. if (of_property_read_bool(dev->of_node, bool_opts[i].name))
  304. *bool_opts[i].val_ptr = true;
  305. }
  306. cfg->num_strings = cfg->num_strings + 1;
  307. return 0;
  308. }
  309. static const struct backlight_ops pm8941_wled_ops = {
  310. .update_status = pm8941_wled_update_status,
  311. };
  312. static int pm8941_wled_probe(struct platform_device *pdev)
  313. {
  314. struct backlight_properties props;
  315. struct backlight_device *bl;
  316. struct pm8941_wled *wled;
  317. struct regmap *regmap;
  318. u32 val;
  319. int rc;
  320. regmap = dev_get_regmap(pdev->dev.parent, NULL);
  321. if (!regmap) {
  322. dev_err(&pdev->dev, "Unable to get regmap\n");
  323. return -EINVAL;
  324. }
  325. wled = devm_kzalloc(&pdev->dev, sizeof(*wled), GFP_KERNEL);
  326. if (!wled)
  327. return -ENOMEM;
  328. wled->regmap = regmap;
  329. rc = pm8941_wled_configure(wled, &pdev->dev);
  330. if (rc)
  331. return rc;
  332. rc = pm8941_wled_setup(wled);
  333. if (rc)
  334. return rc;
  335. val = PM8941_WLED_DEFAULT_BRIGHTNESS;
  336. of_property_read_u32(pdev->dev.of_node, "default-brightness", &val);
  337. memset(&props, 0, sizeof(struct backlight_properties));
  338. props.type = BACKLIGHT_RAW;
  339. props.brightness = val;
  340. props.max_brightness = PM8941_WLED_REG_VAL_MAX;
  341. bl = devm_backlight_device_register(&pdev->dev, wled->name,
  342. &pdev->dev, wled,
  343. &pm8941_wled_ops, &props);
  344. return PTR_ERR_OR_ZERO(bl);
  345. };
  346. static const struct of_device_id pm8941_wled_match_table[] = {
  347. { .compatible = "qcom,pm8941-wled" },
  348. {}
  349. };
  350. MODULE_DEVICE_TABLE(of, pm8941_wled_match_table);
  351. static struct platform_driver pm8941_wled_driver = {
  352. .probe = pm8941_wled_probe,
  353. .driver = {
  354. .name = "pm8941-wled",
  355. .of_match_table = pm8941_wled_match_table,
  356. },
  357. };
  358. module_platform_driver(pm8941_wled_driver);
  359. MODULE_DESCRIPTION("pm8941 wled driver");
  360. MODULE_LICENSE("GPL v2");