arcxcnn_bl.c 11 KB

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