arcxcnn_bl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 = bl->props.brightness;
  117. int ret;
  118. if (bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
  119. brightness = 0;
  120. ret = arcxcnn_set_brightness(lp, brightness);
  121. if (ret)
  122. return ret;
  123. /* set power-on/off/save modes */
  124. return arcxcnn_update_field(lp, ARCXCNN_CMD, ARCXCNN_CMD_STDBY,
  125. (bl->props.power == 0) ? 0 : ARCXCNN_CMD_STDBY);
  126. }
  127. static const struct backlight_ops arcxcnn_bl_ops = {
  128. .options = BL_CORE_SUSPENDRESUME,
  129. .update_status = arcxcnn_bl_update_status,
  130. };
  131. static int arcxcnn_backlight_register(struct arcxcnn *lp)
  132. {
  133. struct backlight_properties *props;
  134. const char *name = lp->pdata->name ? : "arctic_bl";
  135. props = devm_kzalloc(lp->dev, sizeof(*props), GFP_KERNEL);
  136. if (!props)
  137. return -ENOMEM;
  138. props->type = BACKLIGHT_PLATFORM;
  139. props->max_brightness = MAX_BRIGHTNESS;
  140. if (lp->pdata->initial_brightness > props->max_brightness)
  141. lp->pdata->initial_brightness = props->max_brightness;
  142. props->brightness = lp->pdata->initial_brightness;
  143. lp->bl = devm_backlight_device_register(lp->dev, name, lp->dev, lp,
  144. &arcxcnn_bl_ops, props);
  145. return PTR_ERR_OR_ZERO(lp->bl);
  146. }
  147. static void arcxcnn_parse_dt(struct arcxcnn *lp)
  148. {
  149. struct device *dev = lp->dev;
  150. struct device_node *node = dev->of_node;
  151. u32 prog_val, num_entry, entry, sources[ARCXCNN_LEDEN_BITS];
  152. int ret;
  153. /* device tree entry isn't required, defaults are OK */
  154. if (!node)
  155. return;
  156. ret = of_property_read_string(node, "label", &lp->pdata->name);
  157. if (ret < 0)
  158. lp->pdata->name = NULL;
  159. ret = of_property_read_u32(node, "default-brightness", &prog_val);
  160. if (ret == 0)
  161. lp->pdata->initial_brightness = prog_val;
  162. ret = of_property_read_u32(node, "arc,led-config-0", &prog_val);
  163. if (ret == 0)
  164. lp->pdata->led_config_0 = (u8)prog_val;
  165. ret = of_property_read_u32(node, "arc,led-config-1", &prog_val);
  166. if (ret == 0)
  167. lp->pdata->led_config_1 = (u8)prog_val;
  168. ret = of_property_read_u32(node, "arc,dim-freq", &prog_val);
  169. if (ret == 0)
  170. lp->pdata->dim_freq = (u8)prog_val;
  171. ret = of_property_read_u32(node, "arc,comp-config", &prog_val);
  172. if (ret == 0)
  173. lp->pdata->comp_config = (u8)prog_val;
  174. ret = of_property_read_u32(node, "arc,filter-config", &prog_val);
  175. if (ret == 0)
  176. lp->pdata->filter_config = (u8)prog_val;
  177. ret = of_property_read_u32(node, "arc,trim-config", &prog_val);
  178. if (ret == 0)
  179. lp->pdata->trim_config = (u8)prog_val;
  180. ret = of_property_count_u32_elems(node, "led-sources");
  181. if (ret < 0) {
  182. lp->pdata->leden = ARCXCNN_LEDEN_MASK; /* all on is default */
  183. } else {
  184. num_entry = ret;
  185. if (num_entry > ARCXCNN_LEDEN_BITS)
  186. num_entry = ARCXCNN_LEDEN_BITS;
  187. ret = of_property_read_u32_array(node, "led-sources", sources,
  188. num_entry);
  189. if (ret < 0) {
  190. dev_err(dev, "led-sources node is invalid.\n");
  191. return;
  192. }
  193. lp->pdata->leden = 0;
  194. /* for each enable in source, set bit in led enable */
  195. for (entry = 0; entry < num_entry; entry++) {
  196. u8 onbit = 1 << sources[entry];
  197. lp->pdata->leden |= onbit;
  198. }
  199. }
  200. }
  201. static int arcxcnn_probe(struct i2c_client *cl, const struct i2c_device_id *id)
  202. {
  203. struct arcxcnn *lp;
  204. int ret;
  205. if (!i2c_check_functionality(cl->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  206. return -EIO;
  207. lp = devm_kzalloc(&cl->dev, sizeof(*lp), GFP_KERNEL);
  208. if (!lp)
  209. return -ENOMEM;
  210. lp->client = cl;
  211. lp->dev = &cl->dev;
  212. lp->pdata = dev_get_platdata(&cl->dev);
  213. /* reset the device */
  214. ret = i2c_smbus_write_byte_data(lp->client,
  215. ARCXCNN_CMD, ARCXCNN_CMD_RESET);
  216. if (ret)
  217. goto probe_err;
  218. if (!lp->pdata) {
  219. lp->pdata = devm_kzalloc(lp->dev,
  220. sizeof(*lp->pdata), GFP_KERNEL);
  221. if (!lp->pdata)
  222. return -ENOMEM;
  223. /* Setup defaults based on power-on defaults */
  224. lp->pdata->name = NULL;
  225. lp->pdata->initial_brightness = INIT_BRIGHT;
  226. lp->pdata->leden = ARCXCNN_LEDEN_MASK;
  227. lp->pdata->led_config_0 = i2c_smbus_read_byte_data(
  228. lp->client, ARCXCNN_FADECTRL);
  229. lp->pdata->led_config_1 = i2c_smbus_read_byte_data(
  230. lp->client, ARCXCNN_ILED_CONFIG);
  231. /* insure dim mode is not default pwm */
  232. lp->pdata->led_config_1 |= ARCXCNN_ILED_DIM_INT;
  233. lp->pdata->dim_freq = i2c_smbus_read_byte_data(
  234. lp->client, ARCXCNN_DIMFREQ);
  235. lp->pdata->comp_config = i2c_smbus_read_byte_data(
  236. lp->client, ARCXCNN_COMP_CONFIG);
  237. lp->pdata->filter_config = i2c_smbus_read_byte_data(
  238. lp->client, ARCXCNN_FILT_CONFIG);
  239. lp->pdata->trim_config = i2c_smbus_read_byte_data(
  240. lp->client, ARCXCNN_IMAXTUNE);
  241. if (IS_ENABLED(CONFIG_OF))
  242. arcxcnn_parse_dt(lp);
  243. }
  244. i2c_set_clientdata(cl, lp);
  245. /* constrain settings to what is possible */
  246. if (lp->pdata->initial_brightness > MAX_BRIGHTNESS)
  247. lp->pdata->initial_brightness = MAX_BRIGHTNESS;
  248. /* set initial brightness */
  249. ret = arcxcnn_set_brightness(lp, lp->pdata->initial_brightness);
  250. if (ret)
  251. goto probe_err;
  252. /* set other register values directly */
  253. ret = i2c_smbus_write_byte_data(lp->client, ARCXCNN_FADECTRL,
  254. lp->pdata->led_config_0);
  255. if (ret)
  256. goto probe_err;
  257. ret = i2c_smbus_write_byte_data(lp->client, ARCXCNN_ILED_CONFIG,
  258. lp->pdata->led_config_1);
  259. if (ret)
  260. goto probe_err;
  261. ret = i2c_smbus_write_byte_data(lp->client, ARCXCNN_DIMFREQ,
  262. lp->pdata->dim_freq);
  263. if (ret)
  264. goto probe_err;
  265. ret = i2c_smbus_write_byte_data(lp->client, ARCXCNN_COMP_CONFIG,
  266. lp->pdata->comp_config);
  267. if (ret)
  268. goto probe_err;
  269. ret = i2c_smbus_write_byte_data(lp->client, ARCXCNN_FILT_CONFIG,
  270. lp->pdata->filter_config);
  271. if (ret)
  272. goto probe_err;
  273. ret = i2c_smbus_write_byte_data(lp->client, ARCXCNN_IMAXTUNE,
  274. lp->pdata->trim_config);
  275. if (ret)
  276. goto probe_err;
  277. /* set initial LED Enables */
  278. arcxcnn_update_field(lp, ARCXCNN_LEDEN,
  279. ARCXCNN_LEDEN_MASK, lp->pdata->leden);
  280. ret = arcxcnn_backlight_register(lp);
  281. if (ret)
  282. goto probe_register_err;
  283. backlight_update_status(lp->bl);
  284. return 0;
  285. probe_register_err:
  286. dev_err(lp->dev,
  287. "failed to register backlight.\n");
  288. probe_err:
  289. dev_err(lp->dev,
  290. "failure ret: %d\n", ret);
  291. return ret;
  292. }
  293. static int arcxcnn_remove(struct i2c_client *cl)
  294. {
  295. struct arcxcnn *lp = i2c_get_clientdata(cl);
  296. /* disable all strings (ignore errors) */
  297. i2c_smbus_write_byte_data(lp->client,
  298. ARCXCNN_LEDEN, 0x00);
  299. /* reset the device (ignore errors) */
  300. i2c_smbus_write_byte_data(lp->client,
  301. ARCXCNN_CMD, ARCXCNN_CMD_RESET);
  302. lp->bl->props.brightness = 0;
  303. backlight_update_status(lp->bl);
  304. return 0;
  305. }
  306. static const struct of_device_id arcxcnn_dt_ids[] = {
  307. { .compatible = "arc,arc2c0608" },
  308. { }
  309. };
  310. MODULE_DEVICE_TABLE(of, arcxcnn_dt_ids);
  311. static const struct i2c_device_id arcxcnn_ids[] = {
  312. {"arc2c0608", ARC2C0608},
  313. { }
  314. };
  315. MODULE_DEVICE_TABLE(i2c, arcxcnn_ids);
  316. static struct i2c_driver arcxcnn_driver = {
  317. .driver = {
  318. .name = "arcxcnn_bl",
  319. .of_match_table = of_match_ptr(arcxcnn_dt_ids),
  320. },
  321. .probe = arcxcnn_probe,
  322. .remove = arcxcnn_remove,
  323. .id_table = arcxcnn_ids,
  324. };
  325. module_i2c_driver(arcxcnn_driver);
  326. MODULE_LICENSE("GPL v2");
  327. MODULE_AUTHOR("Brian Dodge <bdodge@arcticsand.com>");
  328. MODULE_DESCRIPTION("ARCXCNN Backlight driver");