sky81452-backlight.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * sky81452-backlight.c SKY81452 backlight driver
  4. *
  5. * Copyright 2014 Skyworks Solutions Inc.
  6. * Author : Gyungoh Yoo <jack.yoo@skyworksinc.com>
  7. */
  8. #include <linux/backlight.h>
  9. #include <linux/err.h>
  10. #include <linux/gpio/consumer.h>
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/regmap.h>
  17. #include <linux/slab.h>
  18. /* registers */
  19. #define SKY81452_REG0 0x00
  20. #define SKY81452_REG1 0x01
  21. #define SKY81452_REG2 0x02
  22. #define SKY81452_REG4 0x04
  23. #define SKY81452_REG5 0x05
  24. /* bit mask */
  25. #define SKY81452_CS 0xFF
  26. #define SKY81452_EN 0x3F
  27. #define SKY81452_IGPW 0x20
  28. #define SKY81452_PWMMD 0x10
  29. #define SKY81452_PHASE 0x08
  30. #define SKY81452_ILIM 0x04
  31. #define SKY81452_VSHRT 0x03
  32. #define SKY81452_OCP 0x80
  33. #define SKY81452_OTMP 0x40
  34. #define SKY81452_SHRT 0x3F
  35. #define SKY81452_OPN 0x3F
  36. #define SKY81452_DEFAULT_NAME "lcd-backlight"
  37. #define SKY81452_MAX_BRIGHTNESS (SKY81452_CS + 1)
  38. /**
  39. * struct sky81452_platform_data
  40. * @name: backlight driver name.
  41. * If it is not defined, default name is lcd-backlight.
  42. * @gpiod_enable:GPIO descriptor which control EN pin
  43. * @enable: Enable mask for current sink channel 1, 2, 3, 4, 5 and 6.
  44. * @ignore_pwm: true if DPWMI should be ignored.
  45. * @dpwm_mode: true is DPWM dimming mode, otherwise Analog dimming mode.
  46. * @phase_shift:true is phase shift mode.
  47. * @short_detection_threshold: It should be one of 4, 5, 6 and 7V.
  48. * @boost_current_limit: It should be one of 2300, 2750mA.
  49. */
  50. struct sky81452_bl_platform_data {
  51. const char *name;
  52. struct gpio_desc *gpiod_enable;
  53. unsigned int enable;
  54. bool ignore_pwm;
  55. bool dpwm_mode;
  56. bool phase_shift;
  57. unsigned int short_detection_threshold;
  58. unsigned int boost_current_limit;
  59. };
  60. #define CTZ(b) __builtin_ctz(b)
  61. static int sky81452_bl_update_status(struct backlight_device *bd)
  62. {
  63. const struct sky81452_bl_platform_data *pdata =
  64. dev_get_platdata(bd->dev.parent);
  65. const unsigned int brightness = (unsigned int)bd->props.brightness;
  66. struct regmap *regmap = bl_get_data(bd);
  67. int ret;
  68. if (brightness > 0) {
  69. ret = regmap_write(regmap, SKY81452_REG0, brightness - 1);
  70. if (ret < 0)
  71. return ret;
  72. return regmap_update_bits(regmap, SKY81452_REG1, SKY81452_EN,
  73. pdata->enable << CTZ(SKY81452_EN));
  74. }
  75. return regmap_update_bits(regmap, SKY81452_REG1, SKY81452_EN, 0);
  76. }
  77. static const struct backlight_ops sky81452_bl_ops = {
  78. .update_status = sky81452_bl_update_status,
  79. };
  80. static ssize_t sky81452_bl_store_enable(struct device *dev,
  81. struct device_attribute *attr, const char *buf, size_t count)
  82. {
  83. struct regmap *regmap = bl_get_data(to_backlight_device(dev));
  84. unsigned long value;
  85. int ret;
  86. ret = kstrtoul(buf, 16, &value);
  87. if (ret < 0)
  88. return ret;
  89. ret = regmap_update_bits(regmap, SKY81452_REG1, SKY81452_EN,
  90. value << CTZ(SKY81452_EN));
  91. if (ret < 0)
  92. return ret;
  93. return count;
  94. }
  95. static ssize_t sky81452_bl_show_open_short(struct device *dev,
  96. struct device_attribute *attr, char *buf)
  97. {
  98. struct regmap *regmap = bl_get_data(to_backlight_device(dev));
  99. unsigned int reg, value = 0;
  100. char tmp[3];
  101. int i, ret;
  102. reg = !strcmp(attr->attr.name, "open") ? SKY81452_REG5 : SKY81452_REG4;
  103. ret = regmap_read(regmap, reg, &value);
  104. if (ret < 0)
  105. return ret;
  106. if (value & SKY81452_SHRT) {
  107. *buf = 0;
  108. for (i = 0; i < 6; i++) {
  109. if (value & 0x01) {
  110. sprintf(tmp, "%d ", i + 1);
  111. strcat(buf, tmp);
  112. }
  113. value >>= 1;
  114. }
  115. strcat(buf, "\n");
  116. } else {
  117. strcpy(buf, "none\n");
  118. }
  119. return strlen(buf);
  120. }
  121. static ssize_t sky81452_bl_show_fault(struct device *dev,
  122. struct device_attribute *attr, char *buf)
  123. {
  124. struct regmap *regmap = bl_get_data(to_backlight_device(dev));
  125. unsigned int value = 0;
  126. int ret;
  127. ret = regmap_read(regmap, SKY81452_REG4, &value);
  128. if (ret < 0)
  129. return ret;
  130. *buf = 0;
  131. if (value & SKY81452_OCP)
  132. strcat(buf, "over-current ");
  133. if (value & SKY81452_OTMP)
  134. strcat(buf, "over-temperature");
  135. strcat(buf, "\n");
  136. return strlen(buf);
  137. }
  138. static DEVICE_ATTR(enable, S_IWGRP | S_IWUSR, NULL, sky81452_bl_store_enable);
  139. static DEVICE_ATTR(open, S_IRUGO, sky81452_bl_show_open_short, NULL);
  140. static DEVICE_ATTR(short, S_IRUGO, sky81452_bl_show_open_short, NULL);
  141. static DEVICE_ATTR(fault, S_IRUGO, sky81452_bl_show_fault, NULL);
  142. static struct attribute *sky81452_bl_attribute[] = {
  143. &dev_attr_enable.attr,
  144. &dev_attr_open.attr,
  145. &dev_attr_short.attr,
  146. &dev_attr_fault.attr,
  147. NULL
  148. };
  149. static const struct attribute_group sky81452_bl_attr_group = {
  150. .attrs = sky81452_bl_attribute,
  151. };
  152. #ifdef CONFIG_OF
  153. static struct sky81452_bl_platform_data *sky81452_bl_parse_dt(
  154. struct device *dev)
  155. {
  156. struct device_node *np = of_node_get(dev->of_node);
  157. struct sky81452_bl_platform_data *pdata;
  158. int num_entry;
  159. unsigned int sources[6];
  160. int ret;
  161. if (!np) {
  162. dev_err(dev, "backlight node not found.\n");
  163. return ERR_PTR(-ENODATA);
  164. }
  165. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  166. if (!pdata) {
  167. of_node_put(np);
  168. return ERR_PTR(-ENOMEM);
  169. }
  170. of_property_read_string(np, "name", &pdata->name);
  171. pdata->ignore_pwm = of_property_read_bool(np, "skyworks,ignore-pwm");
  172. pdata->dpwm_mode = of_property_read_bool(np, "skyworks,dpwm-mode");
  173. pdata->phase_shift = of_property_read_bool(np, "skyworks,phase-shift");
  174. pdata->gpiod_enable = devm_gpiod_get_optional(dev, NULL, GPIOD_OUT_HIGH);
  175. ret = of_property_count_u32_elems(np, "led-sources");
  176. if (ret < 0) {
  177. pdata->enable = SKY81452_EN >> CTZ(SKY81452_EN);
  178. } else {
  179. num_entry = ret;
  180. if (num_entry > 6)
  181. num_entry = 6;
  182. ret = of_property_read_u32_array(np, "led-sources", sources,
  183. num_entry);
  184. if (ret < 0) {
  185. dev_err(dev, "led-sources node is invalid.\n");
  186. of_node_put(np);
  187. return ERR_PTR(-EINVAL);
  188. }
  189. pdata->enable = 0;
  190. while (--num_entry)
  191. pdata->enable |= (1 << sources[num_entry]);
  192. }
  193. ret = of_property_read_u32(np,
  194. "skyworks,short-detection-threshold-volt",
  195. &pdata->short_detection_threshold);
  196. if (ret < 0)
  197. pdata->short_detection_threshold = 7;
  198. ret = of_property_read_u32(np, "skyworks,current-limit-mA",
  199. &pdata->boost_current_limit);
  200. if (ret < 0)
  201. pdata->boost_current_limit = 2750;
  202. of_node_put(np);
  203. return pdata;
  204. }
  205. #else
  206. static struct sky81452_bl_platform_data *sky81452_bl_parse_dt(
  207. struct device *dev)
  208. {
  209. return ERR_PTR(-EINVAL);
  210. }
  211. #endif
  212. static int sky81452_bl_init_device(struct regmap *regmap,
  213. struct sky81452_bl_platform_data *pdata)
  214. {
  215. unsigned int value;
  216. value = pdata->ignore_pwm ? SKY81452_IGPW : 0;
  217. value |= pdata->dpwm_mode ? SKY81452_PWMMD : 0;
  218. value |= pdata->phase_shift ? 0 : SKY81452_PHASE;
  219. if (pdata->boost_current_limit == 2300)
  220. value |= SKY81452_ILIM;
  221. else if (pdata->boost_current_limit != 2750)
  222. return -EINVAL;
  223. if (pdata->short_detection_threshold < 4 ||
  224. pdata->short_detection_threshold > 7)
  225. return -EINVAL;
  226. value |= (7 - pdata->short_detection_threshold) << CTZ(SKY81452_VSHRT);
  227. return regmap_write(regmap, SKY81452_REG2, value);
  228. }
  229. static int sky81452_bl_probe(struct platform_device *pdev)
  230. {
  231. struct device *dev = &pdev->dev;
  232. struct regmap *regmap = dev_get_drvdata(dev->parent);
  233. struct sky81452_bl_platform_data *pdata;
  234. struct backlight_device *bd;
  235. struct backlight_properties props;
  236. const char *name;
  237. int ret;
  238. pdata = sky81452_bl_parse_dt(dev);
  239. if (IS_ERR(pdata))
  240. return PTR_ERR(pdata);
  241. ret = sky81452_bl_init_device(regmap, pdata);
  242. if (ret < 0) {
  243. dev_err(dev, "failed to initialize. err=%d\n", ret);
  244. return ret;
  245. }
  246. memset(&props, 0, sizeof(props));
  247. props.max_brightness = SKY81452_MAX_BRIGHTNESS,
  248. name = pdata->name ? pdata->name : SKY81452_DEFAULT_NAME;
  249. bd = devm_backlight_device_register(dev, name, dev, regmap,
  250. &sky81452_bl_ops, &props);
  251. if (IS_ERR(bd)) {
  252. dev_err(dev, "failed to register. err=%ld\n", PTR_ERR(bd));
  253. return PTR_ERR(bd);
  254. }
  255. platform_set_drvdata(pdev, bd);
  256. ret = sysfs_create_group(&bd->dev.kobj, &sky81452_bl_attr_group);
  257. if (ret < 0) {
  258. dev_err(dev, "failed to create attribute. err=%d\n", ret);
  259. return ret;
  260. }
  261. return ret;
  262. }
  263. static int sky81452_bl_remove(struct platform_device *pdev)
  264. {
  265. const struct sky81452_bl_platform_data *pdata =
  266. dev_get_platdata(&pdev->dev);
  267. struct backlight_device *bd = platform_get_drvdata(pdev);
  268. sysfs_remove_group(&bd->dev.kobj, &sky81452_bl_attr_group);
  269. bd->props.power = FB_BLANK_UNBLANK;
  270. bd->props.brightness = 0;
  271. backlight_update_status(bd);
  272. if (pdata->gpiod_enable)
  273. gpiod_set_value_cansleep(pdata->gpiod_enable, 0);
  274. return 0;
  275. }
  276. #ifdef CONFIG_OF
  277. static const struct of_device_id sky81452_bl_of_match[] = {
  278. { .compatible = "skyworks,sky81452-backlight", },
  279. { }
  280. };
  281. MODULE_DEVICE_TABLE(of, sky81452_bl_of_match);
  282. #endif
  283. static struct platform_driver sky81452_bl_driver = {
  284. .driver = {
  285. .name = "sky81452-backlight",
  286. .of_match_table = of_match_ptr(sky81452_bl_of_match),
  287. },
  288. .probe = sky81452_bl_probe,
  289. .remove = sky81452_bl_remove,
  290. };
  291. module_platform_driver(sky81452_bl_driver);
  292. MODULE_DESCRIPTION("Skyworks SKY81452 backlight driver");
  293. MODULE_AUTHOR("Gyungoh Yoo <jack.yoo@skyworksinc.com>");
  294. MODULE_LICENSE("GPL v2");