arcxcnn_bl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Backlight driver for ArcticSand ARC_X_C_0N_0N Devices
  4. *
  5. * Copyright 2016 ArcticSand, Inc.
  6. * Author : Brian Dodge <bdodge@arcticsand.com>
  7. */
  8. #include <linux/backlight.h>
  9. #include <linux/err.h>
  10. #include <linux/i2c.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/slab.h>
  14. enum arcxcnn_chip_id {
  15. ARC2C0608
  16. };
  17. /**
  18. * struct arcxcnn_platform_data
  19. * @name : Backlight driver name (NULL will use default)
  20. * @initial_brightness : initial value of backlight brightness
  21. * @leden : initial LED string enables, upper bit is global on/off
  22. * @led_config_0 : fading speed (period between intensity steps)
  23. * @led_config_1 : misc settings, see datasheet
  24. * @dim_freq : pwm dimming frequency if in pwm mode
  25. * @comp_config : misc config, see datasheet
  26. * @filter_config : RC/PWM filter config, see datasheet
  27. * @trim_config : full scale current trim, see datasheet
  28. */
  29. struct arcxcnn_platform_data {
  30. const char *name;
  31. u16 initial_brightness;
  32. u8 leden;
  33. u8 led_config_0;
  34. u8 led_config_1;
  35. u8 dim_freq;
  36. u8 comp_config;
  37. u8 filter_config;
  38. u8 trim_config;
  39. };
  40. #define ARCXCNN_CMD 0x00 /* Command Register */
  41. #define ARCXCNN_CMD_STDBY 0x80 /* I2C Standby */
  42. #define ARCXCNN_CMD_RESET 0x40 /* Reset */
  43. #define ARCXCNN_CMD_BOOST 0x10 /* Boost */
  44. #define ARCXCNN_CMD_OVP_MASK 0x0C /* --- Over Voltage Threshold */
  45. #define ARCXCNN_CMD_OVP_XXV 0x0C /* <rsvrd> Over Voltage Threshold */
  46. #define ARCXCNN_CMD_OVP_20V 0x08 /* 20v Over Voltage Threshold */
  47. #define ARCXCNN_CMD_OVP_24V 0x04 /* 24v Over Voltage Threshold */
  48. #define ARCXCNN_CMD_OVP_31V 0x00 /* 31.4v Over Voltage Threshold */
  49. #define ARCXCNN_CMD_EXT_COMP 0x01 /* part (0) or full (1) ext. comp */
  50. #define ARCXCNN_CONFIG 0x01 /* Configuration */
  51. #define ARCXCNN_STATUS1 0x02 /* Status 1 */
  52. #define ARCXCNN_STATUS2 0x03 /* Status 2 */
  53. #define ARCXCNN_FADECTRL 0x04 /* Fading Control */
  54. #define ARCXCNN_ILED_CONFIG 0x05 /* ILED Configuration */
  55. #define ARCXCNN_ILED_DIM_PWM 0x00 /* config dim mode pwm */
  56. #define ARCXCNN_ILED_DIM_INT 0x04 /* config dim mode internal */
  57. #define ARCXCNN_LEDEN 0x06 /* LED Enable Register */
  58. #define ARCXCNN_LEDEN_ISETEXT 0x80 /* Full-scale current set extern */
  59. #define ARCXCNN_LEDEN_MASK 0x3F /* LED string enables mask */
  60. #define ARCXCNN_LEDEN_BITS 0x06 /* Bits of LED string enables */
  61. #define ARCXCNN_LEDEN_LED1 0x01
  62. #define ARCXCNN_LEDEN_LED2 0x02
  63. #define ARCXCNN_LEDEN_LED3 0x04
  64. #define ARCXCNN_LEDEN_LED4 0x08
  65. #define ARCXCNN_LEDEN_LED5 0x10
  66. #define ARCXCNN_LEDEN_LED6 0x20
  67. #define ARCXCNN_WLED_ISET_LSB 0x07 /* LED ISET LSB (in upper nibble) */
  68. #define ARCXCNN_WLED_ISET_LSB_SHIFT 0x04 /* ISET LSB Left Shift */
  69. #define ARCXCNN_WLED_ISET_MSB 0x08 /* LED ISET MSB (8 bits) */
  70. #define ARCXCNN_DIMFREQ 0x09
  71. #define ARCXCNN_COMP_CONFIG 0x0A
  72. #define ARCXCNN_FILT_CONFIG 0x0B
  73. #define ARCXCNN_IMAXTUNE 0x0C
  74. #define ARCXCNN_ID_MSB 0x1E
  75. #define ARCXCNN_ID_LSB 0x1F
  76. #define MAX_BRIGHTNESS 4095
  77. #define INIT_BRIGHT 60
  78. struct arcxcnn {
  79. struct i2c_client *client;
  80. struct backlight_device *bl;
  81. struct device *dev;
  82. struct arcxcnn_platform_data *pdata;
  83. };
  84. static int arcxcnn_update_field(struct arcxcnn *lp, u8 reg, u8 mask, u8 data)
  85. {
  86. int ret;
  87. u8 tmp;
  88. ret = i2c_smbus_read_byte_data(lp->client, reg);
  89. if (ret < 0) {
  90. dev_err(lp->dev, "failed to read 0x%.2x\n", reg);
  91. return ret;
  92. }
  93. tmp = (u8)ret;
  94. tmp &= ~mask;
  95. tmp |= data & mask;
  96. return i2c_smbus_write_byte_data(lp->client, reg, tmp);
  97. }
  98. static int arcxcnn_set_brightness(struct arcxcnn *lp, u32 brightness)
  99. {
  100. int ret;
  101. u8 val;
  102. /* lower nibble of brightness goes in upper nibble of LSB register */
  103. val = (brightness & 0xF) << ARCXCNN_WLED_ISET_LSB_SHIFT;
  104. ret = i2c_smbus_write_byte_data(lp->client,
  105. ARCXCNN_WLED_ISET_LSB, val);
  106. if (ret < 0)
  107. return ret;
  108. /* remaining 8 bits of brightness go in MSB register */
  109. val = (brightness >> 4);
  110. return i2c_smbus_write_byte_data(lp->client,
  111. ARCXCNN_WLED_ISET_MSB, val);
  112. }
  113. static int arcxcnn_bl_update_status(struct backlight_device *bl)
  114. {
  115. struct arcxcnn *lp = bl_get_data(bl);
  116. u32 brightness = backlight_get_brightness(bl);
  117. int ret;
  118. ret = arcxcnn_set_brightness(lp, brightness);
  119. if (ret)
  120. return ret;
  121. /* set power-on/off/save modes */
  122. return arcxcnn_update_field(lp, ARCXCNN_CMD, ARCXCNN_CMD_STDBY,
  123. (bl->props.power == 0) ? 0 : ARCXCNN_CMD_STDBY);
  124. }
  125. static const struct backlight_ops arcxcnn_bl_ops = {
  126. .options = BL_CORE_SUSPENDRESUME,
  127. .update_status = arcxcnn_bl_update_status,
  128. };
  129. static int arcxcnn_backlight_register(struct arcxcnn *lp)
  130. {
  131. struct backlight_properties *props;
  132. const char *name = lp->pdata->name ? : "arctic_bl";
  133. props = devm_kzalloc(lp->dev, sizeof(*props), GFP_KERNEL);
  134. if (!props)
  135. return -ENOMEM;
  136. props->type = BACKLIGHT_PLATFORM;
  137. props->max_brightness = MAX_BRIGHTNESS;
  138. if (lp->pdata->initial_brightness > props->max_brightness)
  139. lp->pdata->initial_brightness = props->max_brightness;
  140. props->brightness = lp->pdata->initial_brightness;
  141. lp->bl = devm_backlight_device_register(lp->dev, name, lp->dev, lp,
  142. &arcxcnn_bl_ops, props);
  143. return PTR_ERR_OR_ZERO(lp->bl);
  144. }
  145. static void arcxcnn_parse_dt(struct arcxcnn *lp)
  146. {
  147. struct device *dev = lp->dev;
  148. struct device_node *node = dev->of_node;
  149. u32 prog_val, num_entry, entry, sources[ARCXCNN_LEDEN_BITS];
  150. int ret;
  151. /* device tree entry isn't required, defaults are OK */
  152. if (!node)
  153. return;
  154. ret = of_property_read_string(node, "label", &lp->pdata->name);
  155. if (ret < 0)
  156. lp->pdata->name = NULL;
  157. ret = of_property_read_u32(node, "default-brightness", &prog_val);
  158. if (ret == 0)
  159. lp->pdata->initial_brightness = prog_val;
  160. ret = of_property_read_u32(node, "arc,led-config-0", &prog_val);
  161. if (ret == 0)
  162. lp->pdata->led_config_0 = (u8)prog_val;
  163. ret = of_property_read_u32(node, "arc,led-config-1", &prog_val);
  164. if (ret == 0)
  165. lp->pdata->led_config_1 = (u8)prog_val;
  166. ret = of_property_read_u32(node, "arc,dim-freq", &prog_val);
  167. if (ret == 0)
  168. lp->pdata->dim_freq = (u8)prog_val;
  169. ret = of_property_read_u32(node, "arc,comp-config", &prog_val);
  170. if (ret == 0)
  171. lp->pdata->comp_config = (u8)prog_val;
  172. ret = of_property_read_u32(node, "arc,filter-config", &prog_val);
  173. if (ret == 0)
  174. lp->pdata->filter_config = (u8)prog_val;
  175. ret = of_property_read_u32(node, "arc,trim-config", &prog_val);
  176. if (ret == 0)
  177. lp->pdata->trim_config = (u8)prog_val;
  178. ret = of_property_count_u32_elems(node, "led-sources");
  179. if (ret < 0) {
  180. lp->pdata->leden = ARCXCNN_LEDEN_MASK; /* all on is default */
  181. } else {
  182. num_entry = ret;
  183. if (num_entry > ARCXCNN_LEDEN_BITS)
  184. num_entry = ARCXCNN_LEDEN_BITS;
  185. ret = of_property_read_u32_array(node, "led-sources", sources,
  186. num_entry);
  187. if (ret < 0) {
  188. dev_err(dev, "led-sources node is invalid.\n");
  189. return;
  190. }
  191. lp->pdata->leden = 0;
  192. /* for each enable in source, set bit in led enable */
  193. for (entry = 0; entry < num_entry; entry++) {
  194. u8 onbit = 1 << sources[entry];
  195. lp->pdata->leden |= onbit;
  196. }
  197. }
  198. }
  199. static int arcxcnn_probe(struct i2c_client *cl)
  200. {
  201. struct arcxcnn *lp;
  202. int ret;
  203. if (!i2c_check_functionality(cl->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  204. return -EIO;
  205. lp = devm_kzalloc(&cl->dev, sizeof(*lp), GFP_KERNEL);
  206. if (!lp)
  207. return -ENOMEM;
  208. lp->client = cl;
  209. lp->dev = &cl->dev;
  210. lp->pdata = dev_get_platdata(&cl->dev);
  211. /* reset the device */
  212. ret = i2c_smbus_write_byte_data(lp->client,
  213. ARCXCNN_CMD, ARCXCNN_CMD_RESET);
  214. if (ret)
  215. goto probe_err;
  216. if (!lp->pdata) {
  217. lp->pdata = devm_kzalloc(lp->dev,
  218. sizeof(*lp->pdata), GFP_KERNEL);
  219. if (!lp->pdata)
  220. return -ENOMEM;
  221. /* Setup defaults based on power-on defaults */
  222. lp->pdata->name = NULL;
  223. lp->pdata->initial_brightness = INIT_BRIGHT;
  224. lp->pdata->leden = ARCXCNN_LEDEN_MASK;
  225. lp->pdata->led_config_0 = i2c_smbus_read_byte_data(
  226. lp->client, ARCXCNN_FADECTRL);
  227. lp->pdata->led_config_1 = i2c_smbus_read_byte_data(
  228. lp->client, ARCXCNN_ILED_CONFIG);
  229. /* insure dim mode is not default pwm */
  230. lp->pdata->led_config_1 |= ARCXCNN_ILED_DIM_INT;
  231. lp->pdata->dim_freq = i2c_smbus_read_byte_data(
  232. lp->client, ARCXCNN_DIMFREQ);
  233. lp->pdata->comp_config = i2c_smbus_read_byte_data(
  234. lp->client, ARCXCNN_COMP_CONFIG);
  235. lp->pdata->filter_config = i2c_smbus_read_byte_data(
  236. lp->client, ARCXCNN_FILT_CONFIG);
  237. lp->pdata->trim_config = i2c_smbus_read_byte_data(
  238. lp->client, ARCXCNN_IMAXTUNE);
  239. if (IS_ENABLED(CONFIG_OF))
  240. arcxcnn_parse_dt(lp);
  241. }
  242. i2c_set_clientdata(cl, lp);
  243. /* constrain settings to what is possible */
  244. if (lp->pdata->initial_brightness > MAX_BRIGHTNESS)
  245. lp->pdata->initial_brightness = MAX_BRIGHTNESS;
  246. /* set initial brightness */
  247. ret = arcxcnn_set_brightness(lp, lp->pdata->initial_brightness);
  248. if (ret)
  249. goto probe_err;
  250. /* set other register values directly */
  251. ret = i2c_smbus_write_byte_data(lp->client, ARCXCNN_FADECTRL,
  252. lp->pdata->led_config_0);
  253. if (ret)
  254. goto probe_err;
  255. ret = i2c_smbus_write_byte_data(lp->client, ARCXCNN_ILED_CONFIG,
  256. lp->pdata->led_config_1);
  257. if (ret)
  258. goto probe_err;
  259. ret = i2c_smbus_write_byte_data(lp->client, ARCXCNN_DIMFREQ,
  260. lp->pdata->dim_freq);
  261. if (ret)
  262. goto probe_err;
  263. ret = i2c_smbus_write_byte_data(lp->client, ARCXCNN_COMP_CONFIG,
  264. lp->pdata->comp_config);
  265. if (ret)
  266. goto probe_err;
  267. ret = i2c_smbus_write_byte_data(lp->client, ARCXCNN_FILT_CONFIG,
  268. lp->pdata->filter_config);
  269. if (ret)
  270. goto probe_err;
  271. ret = i2c_smbus_write_byte_data(lp->client, ARCXCNN_IMAXTUNE,
  272. lp->pdata->trim_config);
  273. if (ret)
  274. goto probe_err;
  275. /* set initial LED Enables */
  276. arcxcnn_update_field(lp, ARCXCNN_LEDEN,
  277. ARCXCNN_LEDEN_MASK, lp->pdata->leden);
  278. ret = arcxcnn_backlight_register(lp);
  279. if (ret)
  280. goto probe_register_err;
  281. backlight_update_status(lp->bl);
  282. return 0;
  283. probe_register_err:
  284. dev_err(lp->dev,
  285. "failed to register backlight.\n");
  286. probe_err:
  287. dev_err(lp->dev,
  288. "failure ret: %d\n", ret);
  289. return ret;
  290. }
  291. static void arcxcnn_remove(struct i2c_client *cl)
  292. {
  293. struct arcxcnn *lp = i2c_get_clientdata(cl);
  294. /* disable all strings (ignore errors) */
  295. i2c_smbus_write_byte_data(lp->client,
  296. ARCXCNN_LEDEN, 0x00);
  297. /* reset the device (ignore errors) */
  298. i2c_smbus_write_byte_data(lp->client,
  299. ARCXCNN_CMD, ARCXCNN_CMD_RESET);
  300. lp->bl->props.brightness = 0;
  301. backlight_update_status(lp->bl);
  302. }
  303. static const struct of_device_id arcxcnn_dt_ids[] = {
  304. { .compatible = "arc,arc2c0608" },
  305. { }
  306. };
  307. MODULE_DEVICE_TABLE(of, arcxcnn_dt_ids);
  308. static const struct i2c_device_id arcxcnn_ids[] = {
  309. {"arc2c0608", ARC2C0608},
  310. { }
  311. };
  312. MODULE_DEVICE_TABLE(i2c, arcxcnn_ids);
  313. static struct i2c_driver arcxcnn_driver = {
  314. .driver = {
  315. .name = "arcxcnn_bl",
  316. .of_match_table = arcxcnn_dt_ids,
  317. },
  318. .probe = arcxcnn_probe,
  319. .remove = arcxcnn_remove,
  320. .id_table = arcxcnn_ids,
  321. };
  322. module_i2c_driver(arcxcnn_driver);
  323. MODULE_LICENSE("GPL v2");
  324. MODULE_AUTHOR("Brian Dodge <bdodge@arcticsand.com>");
  325. MODULE_DESCRIPTION("ARCXCNN Backlight driver");