blob: e72393534b7218ae1a560aeba54a507745d24486 [file] [log] [blame]
Thomas Gleixner03761482019-05-28 09:57:24 -07001// SPDX-License-Identifier: GPL-2.0-only
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -07002/*
3 * Copyright (C) 2011 ST-Ericsson SA.
4 * Copyright (C) 2009 Motorola, Inc.
5 *
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -07006 * Simple driver for National Semiconductor LM3530 Backlight driver chip
7 *
8 * Author: Shreshtha Kumar SAHU <shreshthakumar.sahu@stericsson.com>
9 * based on leds-lm3530.c by Dan Murphy <D.Murphy@motorola.com>
10 */
11
12#include <linux/i2c.h>
13#include <linux/leds.h>
14#include <linux/slab.h>
15#include <linux/platform_device.h>
16#include <linux/input.h>
17#include <linux/led-lm3530.h>
18#include <linux/types.h>
Shreshtha Kumar Sahu9b2da532011-05-24 17:13:28 -070019#include <linux/regulator/consumer.h>
Paul Gortmaker54f4ded2011-07-03 13:56:03 -040020#include <linux/module.h>
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -070021
22#define LM3530_LED_DEV "lcd-backlight"
23#define LM3530_NAME "lm3530-led"
24
25#define LM3530_GEN_CONFIG 0x10
26#define LM3530_ALS_CONFIG 0x20
27#define LM3530_BRT_RAMP_RATE 0x30
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -070028#define LM3530_ALS_IMP_SELECT 0x41
29#define LM3530_BRT_CTRL_REG 0xA0
30#define LM3530_ALS_ZB0_REG 0x60
31#define LM3530_ALS_ZB1_REG 0x61
32#define LM3530_ALS_ZB2_REG 0x62
33#define LM3530_ALS_ZB3_REG 0x63
34#define LM3530_ALS_Z0T_REG 0x70
35#define LM3530_ALS_Z1T_REG 0x71
36#define LM3530_ALS_Z2T_REG 0x72
37#define LM3530_ALS_Z3T_REG 0x73
38#define LM3530_ALS_Z4T_REG 0x74
Kim, Milo8544d0e2012-03-23 15:02:12 -070039#define LM3530_REG_MAX 14
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -070040
41/* General Control Register */
42#define LM3530_EN_I2C_SHIFT (0)
43#define LM3530_RAMP_LAW_SHIFT (1)
44#define LM3530_MAX_CURR_SHIFT (2)
45#define LM3530_EN_PWM_SHIFT (5)
46#define LM3530_PWM_POL_SHIFT (6)
47#define LM3530_EN_PWM_SIMPLE_SHIFT (7)
48
49#define LM3530_ENABLE_I2C (1 << LM3530_EN_I2C_SHIFT)
50#define LM3530_ENABLE_PWM (1 << LM3530_EN_PWM_SHIFT)
51#define LM3530_POL_LOW (1 << LM3530_PWM_POL_SHIFT)
52#define LM3530_ENABLE_PWM_SIMPLE (1 << LM3530_EN_PWM_SIMPLE_SHIFT)
53
54/* ALS Config Register Options */
55#define LM3530_ALS_AVG_TIME_SHIFT (0)
56#define LM3530_EN_ALS_SHIFT (3)
57#define LM3530_ALS_SEL_SHIFT (5)
58
59#define LM3530_ENABLE_ALS (3 << LM3530_EN_ALS_SHIFT)
60
61/* Brightness Ramp Rate Register */
62#define LM3530_BRT_RAMP_FALL_SHIFT (0)
63#define LM3530_BRT_RAMP_RISE_SHIFT (3)
64
65/* ALS Resistor Select */
66#define LM3530_ALS1_IMP_SHIFT (0)
67#define LM3530_ALS2_IMP_SHIFT (4)
68
69/* Zone Boundary Register defaults */
Shreshtha Kumar Sahu40b14452011-07-25 17:13:17 -070070#define LM3530_ALS_ZB_MAX (4)
71#define LM3530_ALS_WINDOW_mV (1000)
72#define LM3530_ALS_OFFSET_mV (4)
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -070073
74/* Zone Target Register defaults */
Shreshtha Kumar Sahu40b14452011-07-25 17:13:17 -070075#define LM3530_DEF_ZT_0 (0x7F)
76#define LM3530_DEF_ZT_1 (0x66)
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -070077#define LM3530_DEF_ZT_2 (0x4C)
Shreshtha Kumar Sahu40b14452011-07-25 17:13:17 -070078#define LM3530_DEF_ZT_3 (0x33)
79#define LM3530_DEF_ZT_4 (0x19)
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -070080
Kim, Milobbe64532012-03-23 15:02:11 -070081/* 7 bits are used for the brightness : LM3530_BRT_CTRL_REG */
82#define MAX_BRIGHTNESS (127)
83
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -070084struct lm3530_mode_map {
85 const char *mode;
86 enum lm3530_mode mode_val;
87};
88
89static struct lm3530_mode_map mode_map[] = {
90 { "man", LM3530_BL_MODE_MANUAL },
91 { "als", LM3530_BL_MODE_ALS },
92 { "pwm", LM3530_BL_MODE_PWM },
93};
94
95/**
96 * struct lm3530_data
97 * @led_dev: led class device
98 * @client: i2c client
99 * @pdata: LM3530 platform data
100 * @mode: mode of operation - manual, ALS, PWM
Shreshtha Kumar Sahu9b2da532011-05-24 17:13:28 -0700101 * @regulator: regulator
Lee Jones2906aec2021-05-28 10:06:23 +0100102 * @brightness: previous brightness value
Shreshtha Kumar Sahu9b2da532011-05-24 17:13:28 -0700103 * @enable: regulator is enabled
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700104 */
105struct lm3530_data {
106 struct led_classdev led_dev;
107 struct i2c_client *client;
108 struct lm3530_platform_data *pdata;
109 enum lm3530_mode mode;
Shreshtha Kumar Sahu9b2da532011-05-24 17:13:28 -0700110 struct regulator *regulator;
111 enum led_brightness brightness;
112 bool enable;
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700113};
114
Kim, Milo6335f8f2012-05-29 15:07:29 -0700115/*
116 * struct lm3530_als_data
117 * @config : value of ALS configuration register
118 * @imp_sel : value of ALS resistor select register
119 * @zone : values of ALS ZB(Zone Boundary) registers
120 */
121struct lm3530_als_data {
122 u8 config;
123 u8 imp_sel;
124 u8 zones[LM3530_ALS_ZB_MAX];
125};
126
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700127static const u8 lm3530_reg[LM3530_REG_MAX] = {
128 LM3530_GEN_CONFIG,
129 LM3530_ALS_CONFIG,
130 LM3530_BRT_RAMP_RATE,
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700131 LM3530_ALS_IMP_SELECT,
132 LM3530_BRT_CTRL_REG,
133 LM3530_ALS_ZB0_REG,
134 LM3530_ALS_ZB1_REG,
135 LM3530_ALS_ZB2_REG,
136 LM3530_ALS_ZB3_REG,
137 LM3530_ALS_Z0T_REG,
138 LM3530_ALS_Z1T_REG,
139 LM3530_ALS_Z2T_REG,
140 LM3530_ALS_Z3T_REG,
141 LM3530_ALS_Z4T_REG,
142};
143
144static int lm3530_get_mode_from_str(const char *str)
145{
146 int i;
147
148 for (i = 0; i < ARRAY_SIZE(mode_map); i++)
149 if (sysfs_streq(str, mode_map[i].mode))
150 return mode_map[i].mode_val;
151
Sachin Kamate0e02162012-09-12 14:34:09 +0800152 return -EINVAL;
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700153}
154
Kim, Milo6335f8f2012-05-29 15:07:29 -0700155static void lm3530_als_configure(struct lm3530_platform_data *pdata,
156 struct lm3530_als_data *als)
157{
158 int i;
159 u32 als_vmin, als_vmax, als_vstep;
160
161 if (pdata->als_vmax == 0) {
162 pdata->als_vmin = 0;
163 pdata->als_vmax = LM3530_ALS_WINDOW_mV;
164 }
165
166 als_vmin = pdata->als_vmin;
167 als_vmax = pdata->als_vmax;
168
169 if ((als_vmax - als_vmin) > LM3530_ALS_WINDOW_mV)
170 pdata->als_vmax = als_vmax = als_vmin + LM3530_ALS_WINDOW_mV;
171
172 /* n zone boundary makes n+1 zones */
173 als_vstep = (als_vmax - als_vmin) / (LM3530_ALS_ZB_MAX + 1);
174
175 for (i = 0; i < LM3530_ALS_ZB_MAX; i++)
176 als->zones[i] = (((als_vmin + LM3530_ALS_OFFSET_mV) +
177 als_vstep + (i * als_vstep)) * LED_FULL) / 1000;
178
179 als->config =
180 (pdata->als_avrg_time << LM3530_ALS_AVG_TIME_SHIFT) |
181 (LM3530_ENABLE_ALS) |
182 (pdata->als_input_mode << LM3530_ALS_SEL_SHIFT);
183
184 als->imp_sel =
185 (pdata->als1_resistor_sel << LM3530_ALS1_IMP_SHIFT) |
186 (pdata->als2_resistor_sel << LM3530_ALS2_IMP_SHIFT);
187}
188
Axel Lin5e3b7c62012-12-19 16:59:43 -0800189static int lm3530_led_enable(struct lm3530_data *drvdata)
190{
191 int ret;
192
193 if (drvdata->enable)
194 return 0;
195
196 ret = regulator_enable(drvdata->regulator);
197 if (ret) {
198 dev_err(drvdata->led_dev.dev, "Failed to enable vin:%d\n", ret);
199 return ret;
200 }
201
202 drvdata->enable = true;
203 return 0;
204}
205
206static void lm3530_led_disable(struct lm3530_data *drvdata)
207{
208 int ret;
209
210 if (!drvdata->enable)
211 return;
212
213 ret = regulator_disable(drvdata->regulator);
214 if (ret) {
215 dev_err(drvdata->led_dev.dev, "Failed to disable vin:%d\n",
216 ret);
217 return;
218 }
219
220 drvdata->enable = false;
221}
222
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700223static int lm3530_init_registers(struct lm3530_data *drvdata)
224{
225 int ret = 0;
226 int i;
227 u8 gen_config;
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700228 u8 brt_ramp;
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700229 u8 brightness;
230 u8 reg_val[LM3530_REG_MAX];
Kim, Milo99ad98d2012-03-23 15:02:13 -0700231 struct lm3530_platform_data *pdata = drvdata->pdata;
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700232 struct i2c_client *client = drvdata->client;
Kim, Milo99ad98d2012-03-23 15:02:13 -0700233 struct lm3530_pwm_data *pwm = &pdata->pwm_data;
Kim, Milo6335f8f2012-05-29 15:07:29 -0700234 struct lm3530_als_data als;
235
236 memset(&als, 0, sizeof(struct lm3530_als_data));
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700237
Kim, Milo99ad98d2012-03-23 15:02:13 -0700238 gen_config = (pdata->brt_ramp_law << LM3530_RAMP_LAW_SHIFT) |
239 ((pdata->max_current & 7) << LM3530_MAX_CURR_SHIFT);
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700240
Axel Lincea694c2012-03-23 15:02:13 -0700241 switch (drvdata->mode) {
242 case LM3530_BL_MODE_MANUAL:
Kim, Milo6335f8f2012-05-29 15:07:29 -0700243 gen_config |= LM3530_ENABLE_I2C;
244 break;
Axel Lincea694c2012-03-23 15:02:13 -0700245 case LM3530_BL_MODE_ALS:
246 gen_config |= LM3530_ENABLE_I2C;
Kim, Milo6335f8f2012-05-29 15:07:29 -0700247 lm3530_als_configure(pdata, &als);
Axel Lincea694c2012-03-23 15:02:13 -0700248 break;
249 case LM3530_BL_MODE_PWM:
250 gen_config |= LM3530_ENABLE_PWM | LM3530_ENABLE_PWM_SIMPLE |
251 (pdata->pwm_pol_hi << LM3530_PWM_POL_SHIFT);
252 break;
253 }
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700254
Kim, Milo99ad98d2012-03-23 15:02:13 -0700255 brt_ramp = (pdata->brt_ramp_fall << LM3530_BRT_RAMP_FALL_SHIFT) |
256 (pdata->brt_ramp_rise << LM3530_BRT_RAMP_RISE_SHIFT);
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700257
Shreshtha Kumar Sahu9b2da532011-05-24 17:13:28 -0700258 if (drvdata->brightness)
259 brightness = drvdata->brightness;
260 else
Kim, Milo99ad98d2012-03-23 15:02:13 -0700261 brightness = drvdata->brightness = pdata->brt_val;
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700262
Kim, Milobbe64532012-03-23 15:02:11 -0700263 if (brightness > drvdata->led_dev.max_brightness)
264 brightness = drvdata->led_dev.max_brightness;
265
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700266 reg_val[0] = gen_config; /* LM3530_GEN_CONFIG */
Kim, Milo6335f8f2012-05-29 15:07:29 -0700267 reg_val[1] = als.config; /* LM3530_ALS_CONFIG */
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700268 reg_val[2] = brt_ramp; /* LM3530_BRT_RAMP_RATE */
Kim, Milo6335f8f2012-05-29 15:07:29 -0700269 reg_val[3] = als.imp_sel; /* LM3530_ALS_IMP_SELECT */
Kim, Milo8544d0e2012-03-23 15:02:12 -0700270 reg_val[4] = brightness; /* LM3530_BRT_CTRL_REG */
Kim, Milo6335f8f2012-05-29 15:07:29 -0700271 reg_val[5] = als.zones[0]; /* LM3530_ALS_ZB0_REG */
272 reg_val[6] = als.zones[1]; /* LM3530_ALS_ZB1_REG */
273 reg_val[7] = als.zones[2]; /* LM3530_ALS_ZB2_REG */
274 reg_val[8] = als.zones[3]; /* LM3530_ALS_ZB3_REG */
Kim, Milo8544d0e2012-03-23 15:02:12 -0700275 reg_val[9] = LM3530_DEF_ZT_0; /* LM3530_ALS_Z0T_REG */
276 reg_val[10] = LM3530_DEF_ZT_1; /* LM3530_ALS_Z1T_REG */
277 reg_val[11] = LM3530_DEF_ZT_2; /* LM3530_ALS_Z2T_REG */
278 reg_val[12] = LM3530_DEF_ZT_3; /* LM3530_ALS_Z3T_REG */
279 reg_val[13] = LM3530_DEF_ZT_4; /* LM3530_ALS_Z4T_REG */
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700280
Axel Lin5e3b7c62012-12-19 16:59:43 -0800281 ret = lm3530_led_enable(drvdata);
282 if (ret)
283 return ret;
Shreshtha Kumar Sahu9b2da532011-05-24 17:13:28 -0700284
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700285 for (i = 0; i < LM3530_REG_MAX; i++) {
Kim, Milobb982002012-03-23 15:02:12 -0700286 /* do not update brightness register when pwm mode */
287 if (lm3530_reg[i] == LM3530_BRT_CTRL_REG &&
288 drvdata->mode == LM3530_BL_MODE_PWM) {
289 if (pwm->pwm_set_intensity)
290 pwm->pwm_set_intensity(reg_val[i],
291 drvdata->led_dev.max_brightness);
292 continue;
293 }
294
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700295 ret = i2c_smbus_write_byte_data(client,
296 lm3530_reg[i], reg_val[i]);
297 if (ret)
298 break;
299 }
300
301 return ret;
302}
303
304static void lm3530_brightness_set(struct led_classdev *led_cdev,
305 enum led_brightness brt_val)
306{
307 int err;
308 struct lm3530_data *drvdata =
309 container_of(led_cdev, struct lm3530_data, led_dev);
Kim, Milobb982002012-03-23 15:02:12 -0700310 struct lm3530_platform_data *pdata = drvdata->pdata;
311 struct lm3530_pwm_data *pwm = &pdata->pwm_data;
312 u8 max_brightness = led_cdev->max_brightness;
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700313
314 switch (drvdata->mode) {
315 case LM3530_BL_MODE_MANUAL:
316
Shreshtha Kumar Sahu9b2da532011-05-24 17:13:28 -0700317 if (!drvdata->enable) {
318 err = lm3530_init_registers(drvdata);
319 if (err) {
320 dev_err(&drvdata->client->dev,
321 "Register Init failed: %d\n", err);
322 break;
323 }
324 }
325
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700326 /* set the brightness in brightness control register*/
327 err = i2c_smbus_write_byte_data(drvdata->client,
Kim, Milobbe64532012-03-23 15:02:11 -0700328 LM3530_BRT_CTRL_REG, brt_val);
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700329 if (err)
330 dev_err(&drvdata->client->dev,
331 "Unable to set brightness: %d\n", err);
Shreshtha Kumar Sahu9b2da532011-05-24 17:13:28 -0700332 else
Kim, Milobbe64532012-03-23 15:02:11 -0700333 drvdata->brightness = brt_val;
Shreshtha Kumar Sahu9b2da532011-05-24 17:13:28 -0700334
Axel Lin5e3b7c62012-12-19 16:59:43 -0800335 if (brt_val == 0)
336 lm3530_led_disable(drvdata);
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700337 break;
338 case LM3530_BL_MODE_ALS:
339 break;
340 case LM3530_BL_MODE_PWM:
Kim, Milobb982002012-03-23 15:02:12 -0700341 if (pwm->pwm_set_intensity)
342 pwm->pwm_set_intensity(brt_val, max_brightness);
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700343 break;
344 default:
345 break;
346 }
347}
348
Dwaipayan Ray5ccfa392020-12-14 11:12:05 +0530349static ssize_t mode_show(struct device *dev,
350 struct device_attribute *attr, char *buf)
Shreshtha Kumar Sahu40b14452011-07-25 17:13:17 -0700351{
Kim, Miloe13d9782012-03-23 15:02:11 -0700352 struct led_classdev *led_cdev = dev_get_drvdata(dev);
353 struct lm3530_data *drvdata;
Shreshtha Kumar Sahu40b14452011-07-25 17:13:17 -0700354 int i, len = 0;
355
Kim, Miloe13d9782012-03-23 15:02:11 -0700356 drvdata = container_of(led_cdev, struct lm3530_data, led_dev);
Shreshtha Kumar Sahu40b14452011-07-25 17:13:17 -0700357 for (i = 0; i < ARRAY_SIZE(mode_map); i++)
358 if (drvdata->mode == mode_map[i].mode_val)
359 len += sprintf(buf + len, "[%s] ", mode_map[i].mode);
360 else
361 len += sprintf(buf + len, "%s ", mode_map[i].mode);
362
363 len += sprintf(buf + len, "\n");
364
365 return len;
366}
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700367
Dwaipayan Ray5ccfa392020-12-14 11:12:05 +0530368static ssize_t mode_store(struct device *dev, struct device_attribute
369 *attr, const char *buf, size_t size)
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700370{
Kim, Miloe13d9782012-03-23 15:02:11 -0700371 struct led_classdev *led_cdev = dev_get_drvdata(dev);
372 struct lm3530_data *drvdata;
Kim, Milobb982002012-03-23 15:02:12 -0700373 struct lm3530_pwm_data *pwm;
374 u8 max_brightness;
Kim, Miloe13d9782012-03-23 15:02:11 -0700375 int mode, err;
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700376
Kim, Miloe13d9782012-03-23 15:02:11 -0700377 drvdata = container_of(led_cdev, struct lm3530_data, led_dev);
Kim, Milobb982002012-03-23 15:02:12 -0700378 pwm = &drvdata->pdata->pwm_data;
379 max_brightness = led_cdev->max_brightness;
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700380 mode = lm3530_get_mode_from_str(buf);
381 if (mode < 0) {
382 dev_err(dev, "Invalid mode\n");
Sachin Kamate0e02162012-09-12 14:34:09 +0800383 return mode;
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700384 }
385
Kim, Milobb982002012-03-23 15:02:12 -0700386 drvdata->mode = mode;
387
388 /* set pwm to low if unnecessary */
389 if (mode != LM3530_BL_MODE_PWM && pwm->pwm_set_intensity)
390 pwm->pwm_set_intensity(0, max_brightness);
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700391
392 err = lm3530_init_registers(drvdata);
393 if (err) {
394 dev_err(dev, "Setting %s Mode failed :%d\n", buf, err);
395 return err;
396 }
397
398 return sizeof(drvdata->mode);
399}
Dwaipayan Ray5ccfa392020-12-14 11:12:05 +0530400static DEVICE_ATTR_RW(mode);
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700401
Johan Hovold74c85032014-06-25 10:08:45 -0700402static struct attribute *lm3530_attrs[] = {
403 &dev_attr_mode.attr,
404 NULL
405};
406ATTRIBUTE_GROUPS(lm3530);
407
Bill Pemberton98ea1ea2012-11-19 13:23:02 -0500408static int lm3530_probe(struct i2c_client *client,
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700409 const struct i2c_device_id *id)
410{
Jingoo Han87aae1e2013-07-30 01:07:35 -0700411 struct lm3530_platform_data *pdata = dev_get_platdata(&client->dev);
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700412 struct lm3530_data *drvdata;
413 int err = 0;
414
415 if (pdata == NULL) {
416 dev_err(&client->dev, "platform data required\n");
Bryan Wu0b873f32012-07-04 12:24:40 +0800417 return -ENODEV;
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700418 }
419
420 /* BL mode */
421 if (pdata->mode > LM3530_BL_MODE_PWM) {
422 dev_err(&client->dev, "Illegal Mode request\n");
Bryan Wu0b873f32012-07-04 12:24:40 +0800423 return -EINVAL;
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700424 }
425
426 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
427 dev_err(&client->dev, "I2C_FUNC_I2C not supported\n");
Bryan Wu0b873f32012-07-04 12:24:40 +0800428 return -EIO;
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700429 }
430
Bryan Wu0b873f32012-07-04 12:24:40 +0800431 drvdata = devm_kzalloc(&client->dev, sizeof(struct lm3530_data),
432 GFP_KERNEL);
433 if (drvdata == NULL)
434 return -ENOMEM;
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700435
436 drvdata->mode = pdata->mode;
437 drvdata->client = client;
438 drvdata->pdata = pdata;
Shreshtha Kumar Sahu9b2da532011-05-24 17:13:28 -0700439 drvdata->brightness = LED_OFF;
440 drvdata->enable = false;
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700441 drvdata->led_dev.name = LM3530_LED_DEV;
442 drvdata->led_dev.brightness_set = lm3530_brightness_set;
Kim, Milobbe64532012-03-23 15:02:11 -0700443 drvdata->led_dev.max_brightness = MAX_BRIGHTNESS;
Johan Hovold74c85032014-06-25 10:08:45 -0700444 drvdata->led_dev.groups = lm3530_groups;
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700445
446 i2c_set_clientdata(client, drvdata);
447
Sachin Kamata47df972012-09-11 19:42:10 +0800448 drvdata->regulator = devm_regulator_get(&client->dev, "vin");
Shreshtha Kumar Sahu9b2da532011-05-24 17:13:28 -0700449 if (IS_ERR(drvdata->regulator)) {
450 dev_err(&client->dev, "regulator get failed\n");
451 err = PTR_ERR(drvdata->regulator);
452 drvdata->regulator = NULL;
Bryan Wu0b873f32012-07-04 12:24:40 +0800453 return err;
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700454 }
455
Shreshtha Kumar Sahu9b2da532011-05-24 17:13:28 -0700456 if (drvdata->pdata->brt_val) {
457 err = lm3530_init_registers(drvdata);
458 if (err < 0) {
459 dev_err(&client->dev,
460 "Register Init failed: %d\n", err);
Sachin Kamate0e02162012-09-12 14:34:09 +0800461 return err;
Shreshtha Kumar Sahu9b2da532011-05-24 17:13:28 -0700462 }
463 }
464 err = led_classdev_register(&client->dev, &drvdata->led_dev);
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700465 if (err < 0) {
466 dev_err(&client->dev, "Register led class failed: %d\n", err);
Sachin Kamate0e02162012-09-12 14:34:09 +0800467 return err;
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700468 }
469
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700470 return 0;
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700471}
472
Bill Pemberton678e8a62012-11-19 13:26:00 -0500473static int lm3530_remove(struct i2c_client *client)
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700474{
475 struct lm3530_data *drvdata = i2c_get_clientdata(client);
476
Axel Lin5e3b7c62012-12-19 16:59:43 -0800477 lm3530_led_disable(drvdata);
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700478 led_classdev_unregister(&drvdata->led_dev);
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700479 return 0;
480}
481
482static const struct i2c_device_id lm3530_id[] = {
483 {LM3530_NAME, 0},
484 {}
485};
Axel Lind5f33d42011-05-17 15:44:09 -0700486MODULE_DEVICE_TABLE(i2c, lm3530_id);
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700487
488static struct i2c_driver lm3530_i2c_driver = {
489 .probe = lm3530_probe,
Bill Pembertondf07cf82012-11-19 13:20:20 -0500490 .remove = lm3530_remove,
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700491 .id_table = lm3530_id,
492 .driver = {
493 .name = LM3530_NAME,
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700494 },
495};
496
Axel Lin09a0d182012-01-10 15:09:27 -0800497module_i2c_driver(lm3530_i2c_driver);
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700498
499MODULE_DESCRIPTION("Back Light driver for LM3530");
500MODULE_LICENSE("GPL v2");
501MODULE_AUTHOR("Shreshtha Kumar SAHU <shreshthakumar.sahu@stericsson.com>");