10376148fSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2b1e6b706SShreshtha Kumar Sahu /*
3b1e6b706SShreshtha Kumar Sahu * Copyright (C) 2011 ST-Ericsson SA.
4b1e6b706SShreshtha Kumar Sahu * Copyright (C) 2009 Motorola, Inc.
5b1e6b706SShreshtha Kumar Sahu *
6b1e6b706SShreshtha Kumar Sahu * Simple driver for National Semiconductor LM3530 Backlight driver chip
7b1e6b706SShreshtha Kumar Sahu *
8b1e6b706SShreshtha Kumar Sahu * Author: Shreshtha Kumar SAHU <shreshthakumar.sahu@stericsson.com>
9b1e6b706SShreshtha Kumar Sahu * based on leds-lm3530.c by Dan Murphy <D.Murphy@motorola.com>
10b1e6b706SShreshtha Kumar Sahu */
11b1e6b706SShreshtha Kumar Sahu
12b1e6b706SShreshtha Kumar Sahu #include <linux/i2c.h>
13b1e6b706SShreshtha Kumar Sahu #include <linux/leds.h>
14b1e6b706SShreshtha Kumar Sahu #include <linux/slab.h>
15b1e6b706SShreshtha Kumar Sahu #include <linux/platform_device.h>
16b1e6b706SShreshtha Kumar Sahu #include <linux/input.h>
17b1e6b706SShreshtha Kumar Sahu #include <linux/led-lm3530.h>
18b1e6b706SShreshtha Kumar Sahu #include <linux/types.h>
199b2da53fSShreshtha Kumar Sahu #include <linux/regulator/consumer.h>
2054f4dedbSPaul Gortmaker #include <linux/module.h>
21b1e6b706SShreshtha Kumar Sahu
22b1e6b706SShreshtha Kumar Sahu #define LM3530_LED_DEV "lcd-backlight"
23b1e6b706SShreshtha Kumar Sahu #define LM3530_NAME "lm3530-led"
24b1e6b706SShreshtha Kumar Sahu
25b1e6b706SShreshtha Kumar Sahu #define LM3530_GEN_CONFIG 0x10
26b1e6b706SShreshtha Kumar Sahu #define LM3530_ALS_CONFIG 0x20
27b1e6b706SShreshtha Kumar Sahu #define LM3530_BRT_RAMP_RATE 0x30
28b1e6b706SShreshtha Kumar Sahu #define LM3530_ALS_IMP_SELECT 0x41
29b1e6b706SShreshtha Kumar Sahu #define LM3530_BRT_CTRL_REG 0xA0
30b1e6b706SShreshtha Kumar Sahu #define LM3530_ALS_ZB0_REG 0x60
31b1e6b706SShreshtha Kumar Sahu #define LM3530_ALS_ZB1_REG 0x61
32b1e6b706SShreshtha Kumar Sahu #define LM3530_ALS_ZB2_REG 0x62
33b1e6b706SShreshtha Kumar Sahu #define LM3530_ALS_ZB3_REG 0x63
34b1e6b706SShreshtha Kumar Sahu #define LM3530_ALS_Z0T_REG 0x70
35b1e6b706SShreshtha Kumar Sahu #define LM3530_ALS_Z1T_REG 0x71
36b1e6b706SShreshtha Kumar Sahu #define LM3530_ALS_Z2T_REG 0x72
37b1e6b706SShreshtha Kumar Sahu #define LM3530_ALS_Z3T_REG 0x73
38b1e6b706SShreshtha Kumar Sahu #define LM3530_ALS_Z4T_REG 0x74
398544d0e3SKim, Milo #define LM3530_REG_MAX 14
40b1e6b706SShreshtha Kumar Sahu
41b1e6b706SShreshtha Kumar Sahu /* General Control Register */
42b1e6b706SShreshtha Kumar Sahu #define LM3530_EN_I2C_SHIFT (0)
43b1e6b706SShreshtha Kumar Sahu #define LM3530_RAMP_LAW_SHIFT (1)
44b1e6b706SShreshtha Kumar Sahu #define LM3530_MAX_CURR_SHIFT (2)
45b1e6b706SShreshtha Kumar Sahu #define LM3530_EN_PWM_SHIFT (5)
46b1e6b706SShreshtha Kumar Sahu #define LM3530_PWM_POL_SHIFT (6)
47b1e6b706SShreshtha Kumar Sahu #define LM3530_EN_PWM_SIMPLE_SHIFT (7)
48b1e6b706SShreshtha Kumar Sahu
49b1e6b706SShreshtha Kumar Sahu #define LM3530_ENABLE_I2C (1 << LM3530_EN_I2C_SHIFT)
50b1e6b706SShreshtha Kumar Sahu #define LM3530_ENABLE_PWM (1 << LM3530_EN_PWM_SHIFT)
51b1e6b706SShreshtha Kumar Sahu #define LM3530_POL_LOW (1 << LM3530_PWM_POL_SHIFT)
52b1e6b706SShreshtha Kumar Sahu #define LM3530_ENABLE_PWM_SIMPLE (1 << LM3530_EN_PWM_SIMPLE_SHIFT)
53b1e6b706SShreshtha Kumar Sahu
54b1e6b706SShreshtha Kumar Sahu /* ALS Config Register Options */
55b1e6b706SShreshtha Kumar Sahu #define LM3530_ALS_AVG_TIME_SHIFT (0)
56b1e6b706SShreshtha Kumar Sahu #define LM3530_EN_ALS_SHIFT (3)
57b1e6b706SShreshtha Kumar Sahu #define LM3530_ALS_SEL_SHIFT (5)
58b1e6b706SShreshtha Kumar Sahu
59b1e6b706SShreshtha Kumar Sahu #define LM3530_ENABLE_ALS (3 << LM3530_EN_ALS_SHIFT)
60b1e6b706SShreshtha Kumar Sahu
61b1e6b706SShreshtha Kumar Sahu /* Brightness Ramp Rate Register */
62b1e6b706SShreshtha Kumar Sahu #define LM3530_BRT_RAMP_FALL_SHIFT (0)
63b1e6b706SShreshtha Kumar Sahu #define LM3530_BRT_RAMP_RISE_SHIFT (3)
64b1e6b706SShreshtha Kumar Sahu
65b1e6b706SShreshtha Kumar Sahu /* ALS Resistor Select */
66b1e6b706SShreshtha Kumar Sahu #define LM3530_ALS1_IMP_SHIFT (0)
67b1e6b706SShreshtha Kumar Sahu #define LM3530_ALS2_IMP_SHIFT (4)
68b1e6b706SShreshtha Kumar Sahu
69b1e6b706SShreshtha Kumar Sahu /* Zone Boundary Register defaults */
7040b1445bSShreshtha Kumar Sahu #define LM3530_ALS_ZB_MAX (4)
7140b1445bSShreshtha Kumar Sahu #define LM3530_ALS_WINDOW_mV (1000)
7240b1445bSShreshtha Kumar Sahu #define LM3530_ALS_OFFSET_mV (4)
73b1e6b706SShreshtha Kumar Sahu
74b1e6b706SShreshtha Kumar Sahu /* Zone Target Register defaults */
7540b1445bSShreshtha Kumar Sahu #define LM3530_DEF_ZT_0 (0x7F)
7640b1445bSShreshtha Kumar Sahu #define LM3530_DEF_ZT_1 (0x66)
77b1e6b706SShreshtha Kumar Sahu #define LM3530_DEF_ZT_2 (0x4C)
7840b1445bSShreshtha Kumar Sahu #define LM3530_DEF_ZT_3 (0x33)
7940b1445bSShreshtha Kumar Sahu #define LM3530_DEF_ZT_4 (0x19)
80b1e6b706SShreshtha Kumar Sahu
81bbe64532SKim, Milo /* 7 bits are used for the brightness : LM3530_BRT_CTRL_REG */
82bbe64532SKim, Milo #define MAX_BRIGHTNESS (127)
83bbe64532SKim, Milo
84b1e6b706SShreshtha Kumar Sahu struct lm3530_mode_map {
85b1e6b706SShreshtha Kumar Sahu const char *mode;
86b1e6b706SShreshtha Kumar Sahu enum lm3530_mode mode_val;
87b1e6b706SShreshtha Kumar Sahu };
88b1e6b706SShreshtha Kumar Sahu
89b1e6b706SShreshtha Kumar Sahu static struct lm3530_mode_map mode_map[] = {
90b1e6b706SShreshtha Kumar Sahu { "man", LM3530_BL_MODE_MANUAL },
91b1e6b706SShreshtha Kumar Sahu { "als", LM3530_BL_MODE_ALS },
92b1e6b706SShreshtha Kumar Sahu { "pwm", LM3530_BL_MODE_PWM },
93b1e6b706SShreshtha Kumar Sahu };
94b1e6b706SShreshtha Kumar Sahu
95b1e6b706SShreshtha Kumar Sahu /**
96b1e6b706SShreshtha Kumar Sahu * struct lm3530_data
97b1e6b706SShreshtha Kumar Sahu * @led_dev: led class device
98b1e6b706SShreshtha Kumar Sahu * @client: i2c client
99b1e6b706SShreshtha Kumar Sahu * @pdata: LM3530 platform data
100b1e6b706SShreshtha Kumar Sahu * @mode: mode of operation - manual, ALS, PWM
1019b2da53fSShreshtha Kumar Sahu * @regulator: regulator
1022906aecbSLee Jones * @brightness: previous brightness value
1039b2da53fSShreshtha Kumar Sahu * @enable: regulator is enabled
104b1e6b706SShreshtha Kumar Sahu */
105b1e6b706SShreshtha Kumar Sahu struct lm3530_data {
106b1e6b706SShreshtha Kumar Sahu struct led_classdev led_dev;
107b1e6b706SShreshtha Kumar Sahu struct i2c_client *client;
108b1e6b706SShreshtha Kumar Sahu struct lm3530_platform_data *pdata;
109b1e6b706SShreshtha Kumar Sahu enum lm3530_mode mode;
1109b2da53fSShreshtha Kumar Sahu struct regulator *regulator;
1119b2da53fSShreshtha Kumar Sahu enum led_brightness brightness;
1129b2da53fSShreshtha Kumar Sahu bool enable;
113b1e6b706SShreshtha Kumar Sahu };
114b1e6b706SShreshtha Kumar Sahu
1156335f8faSKim, Milo /*
1166335f8faSKim, Milo * struct lm3530_als_data
1176335f8faSKim, Milo * @config : value of ALS configuration register
1186335f8faSKim, Milo * @imp_sel : value of ALS resistor select register
1196335f8faSKim, Milo * @zone : values of ALS ZB(Zone Boundary) registers
1206335f8faSKim, Milo */
1216335f8faSKim, Milo struct lm3530_als_data {
1226335f8faSKim, Milo u8 config;
1236335f8faSKim, Milo u8 imp_sel;
1246335f8faSKim, Milo u8 zones[LM3530_ALS_ZB_MAX];
1256335f8faSKim, Milo };
1266335f8faSKim, Milo
127b1e6b706SShreshtha Kumar Sahu static const u8 lm3530_reg[LM3530_REG_MAX] = {
128b1e6b706SShreshtha Kumar Sahu LM3530_GEN_CONFIG,
129b1e6b706SShreshtha Kumar Sahu LM3530_ALS_CONFIG,
130b1e6b706SShreshtha Kumar Sahu LM3530_BRT_RAMP_RATE,
131b1e6b706SShreshtha Kumar Sahu LM3530_ALS_IMP_SELECT,
132b1e6b706SShreshtha Kumar Sahu LM3530_BRT_CTRL_REG,
133b1e6b706SShreshtha Kumar Sahu LM3530_ALS_ZB0_REG,
134b1e6b706SShreshtha Kumar Sahu LM3530_ALS_ZB1_REG,
135b1e6b706SShreshtha Kumar Sahu LM3530_ALS_ZB2_REG,
136b1e6b706SShreshtha Kumar Sahu LM3530_ALS_ZB3_REG,
137b1e6b706SShreshtha Kumar Sahu LM3530_ALS_Z0T_REG,
138b1e6b706SShreshtha Kumar Sahu LM3530_ALS_Z1T_REG,
139b1e6b706SShreshtha Kumar Sahu LM3530_ALS_Z2T_REG,
140b1e6b706SShreshtha Kumar Sahu LM3530_ALS_Z3T_REG,
141b1e6b706SShreshtha Kumar Sahu LM3530_ALS_Z4T_REG,
142b1e6b706SShreshtha Kumar Sahu };
143b1e6b706SShreshtha Kumar Sahu
lm3530_get_mode_from_str(const char * str)144b1e6b706SShreshtha Kumar Sahu static int lm3530_get_mode_from_str(const char *str)
145b1e6b706SShreshtha Kumar Sahu {
146b1e6b706SShreshtha Kumar Sahu int i;
147b1e6b706SShreshtha Kumar Sahu
148b1e6b706SShreshtha Kumar Sahu for (i = 0; i < ARRAY_SIZE(mode_map); i++)
149b1e6b706SShreshtha Kumar Sahu if (sysfs_streq(str, mode_map[i].mode))
150b1e6b706SShreshtha Kumar Sahu return mode_map[i].mode_val;
151b1e6b706SShreshtha Kumar Sahu
152e0e02160SSachin Kamat return -EINVAL;
153b1e6b706SShreshtha Kumar Sahu }
154b1e6b706SShreshtha Kumar Sahu
lm3530_als_configure(struct lm3530_platform_data * pdata,struct lm3530_als_data * als)1556335f8faSKim, Milo static void lm3530_als_configure(struct lm3530_platform_data *pdata,
1566335f8faSKim, Milo struct lm3530_als_data *als)
157b1e6b706SShreshtha Kumar Sahu {
158b1e6b706SShreshtha Kumar Sahu int i;
15940b1445bSShreshtha Kumar Sahu u32 als_vmin, als_vmax, als_vstep;
160b1e6b706SShreshtha Kumar Sahu
16199ad98d2SKim, Milo if (pdata->als_vmax == 0) {
16299ad98d2SKim, Milo pdata->als_vmin = 0;
16399ad98d2SKim, Milo pdata->als_vmax = LM3530_ALS_WINDOW_mV;
16440b1445bSShreshtha Kumar Sahu }
16540b1445bSShreshtha Kumar Sahu
16699ad98d2SKim, Milo als_vmin = pdata->als_vmin;
16799ad98d2SKim, Milo als_vmax = pdata->als_vmax;
16840b1445bSShreshtha Kumar Sahu
16940b1445bSShreshtha Kumar Sahu if ((als_vmax - als_vmin) > LM3530_ALS_WINDOW_mV)
1706335f8faSKim, Milo pdata->als_vmax = als_vmax = als_vmin + LM3530_ALS_WINDOW_mV;
17140b1445bSShreshtha Kumar Sahu
17240b1445bSShreshtha Kumar Sahu /* n zone boundary makes n+1 zones */
17340b1445bSShreshtha Kumar Sahu als_vstep = (als_vmax - als_vmin) / (LM3530_ALS_ZB_MAX + 1);
17440b1445bSShreshtha Kumar Sahu
17540b1445bSShreshtha Kumar Sahu for (i = 0; i < LM3530_ALS_ZB_MAX; i++)
1766335f8faSKim, Milo als->zones[i] = (((als_vmin + LM3530_ALS_OFFSET_mV) +
1776335f8faSKim, Milo als_vstep + (i * als_vstep)) * LED_FULL) / 1000;
17840b1445bSShreshtha Kumar Sahu
1796335f8faSKim, Milo als->config =
18099ad98d2SKim, Milo (pdata->als_avrg_time << LM3530_ALS_AVG_TIME_SHIFT) |
181b1e6b706SShreshtha Kumar Sahu (LM3530_ENABLE_ALS) |
18299ad98d2SKim, Milo (pdata->als_input_mode << LM3530_ALS_SEL_SHIFT);
183b1e6b706SShreshtha Kumar Sahu
1846335f8faSKim, Milo als->imp_sel =
18599ad98d2SKim, Milo (pdata->als1_resistor_sel << LM3530_ALS1_IMP_SHIFT) |
18699ad98d2SKim, Milo (pdata->als2_resistor_sel << LM3530_ALS2_IMP_SHIFT);
1876335f8faSKim, Milo }
18840b1445bSShreshtha Kumar Sahu
lm3530_led_enable(struct lm3530_data * drvdata)1895e3b7c6bSAxel Lin static int lm3530_led_enable(struct lm3530_data *drvdata)
1905e3b7c6bSAxel Lin {
1915e3b7c6bSAxel Lin int ret;
1925e3b7c6bSAxel Lin
1935e3b7c6bSAxel Lin if (drvdata->enable)
1945e3b7c6bSAxel Lin return 0;
1955e3b7c6bSAxel Lin
1965e3b7c6bSAxel Lin ret = regulator_enable(drvdata->regulator);
1975e3b7c6bSAxel Lin if (ret) {
1985e3b7c6bSAxel Lin dev_err(drvdata->led_dev.dev, "Failed to enable vin:%d\n", ret);
1995e3b7c6bSAxel Lin return ret;
2005e3b7c6bSAxel Lin }
2015e3b7c6bSAxel Lin
2025e3b7c6bSAxel Lin drvdata->enable = true;
2035e3b7c6bSAxel Lin return 0;
2045e3b7c6bSAxel Lin }
2055e3b7c6bSAxel Lin
lm3530_led_disable(struct lm3530_data * drvdata)2065e3b7c6bSAxel Lin static void lm3530_led_disable(struct lm3530_data *drvdata)
2075e3b7c6bSAxel Lin {
2085e3b7c6bSAxel Lin int ret;
2095e3b7c6bSAxel Lin
2105e3b7c6bSAxel Lin if (!drvdata->enable)
2115e3b7c6bSAxel Lin return;
2125e3b7c6bSAxel Lin
2135e3b7c6bSAxel Lin ret = regulator_disable(drvdata->regulator);
2145e3b7c6bSAxel Lin if (ret) {
2155e3b7c6bSAxel Lin dev_err(drvdata->led_dev.dev, "Failed to disable vin:%d\n",
2165e3b7c6bSAxel Lin ret);
2175e3b7c6bSAxel Lin return;
2185e3b7c6bSAxel Lin }
2195e3b7c6bSAxel Lin
2205e3b7c6bSAxel Lin drvdata->enable = false;
2215e3b7c6bSAxel Lin }
2225e3b7c6bSAxel Lin
lm3530_init_registers(struct lm3530_data * drvdata)2236335f8faSKim, Milo static int lm3530_init_registers(struct lm3530_data *drvdata)
2246335f8faSKim, Milo {
2256335f8faSKim, Milo int ret = 0;
2266335f8faSKim, Milo int i;
2276335f8faSKim, Milo u8 gen_config;
2286335f8faSKim, Milo u8 brt_ramp;
2296335f8faSKim, Milo u8 brightness;
2306335f8faSKim, Milo u8 reg_val[LM3530_REG_MAX];
2316335f8faSKim, Milo struct lm3530_platform_data *pdata = drvdata->pdata;
2326335f8faSKim, Milo struct i2c_client *client = drvdata->client;
2336335f8faSKim, Milo struct lm3530_pwm_data *pwm = &pdata->pwm_data;
2346335f8faSKim, Milo struct lm3530_als_data als;
2356335f8faSKim, Milo
2366335f8faSKim, Milo memset(&als, 0, sizeof(struct lm3530_als_data));
2376335f8faSKim, Milo
2386335f8faSKim, Milo gen_config = (pdata->brt_ramp_law << LM3530_RAMP_LAW_SHIFT) |
2396335f8faSKim, Milo ((pdata->max_current & 7) << LM3530_MAX_CURR_SHIFT);
2406335f8faSKim, Milo
2416335f8faSKim, Milo switch (drvdata->mode) {
2426335f8faSKim, Milo case LM3530_BL_MODE_MANUAL:
2436335f8faSKim, Milo gen_config |= LM3530_ENABLE_I2C;
2446335f8faSKim, Milo break;
2456335f8faSKim, Milo case LM3530_BL_MODE_ALS:
2466335f8faSKim, Milo gen_config |= LM3530_ENABLE_I2C;
2476335f8faSKim, Milo lm3530_als_configure(pdata, &als);
2486335f8faSKim, Milo break;
2496335f8faSKim, Milo case LM3530_BL_MODE_PWM:
2506335f8faSKim, Milo gen_config |= LM3530_ENABLE_PWM | LM3530_ENABLE_PWM_SIMPLE |
2516335f8faSKim, Milo (pdata->pwm_pol_hi << LM3530_PWM_POL_SHIFT);
2526335f8faSKim, Milo break;
253b1e6b706SShreshtha Kumar Sahu }
254b1e6b706SShreshtha Kumar Sahu
25599ad98d2SKim, Milo brt_ramp = (pdata->brt_ramp_fall << LM3530_BRT_RAMP_FALL_SHIFT) |
25699ad98d2SKim, Milo (pdata->brt_ramp_rise << LM3530_BRT_RAMP_RISE_SHIFT);
257b1e6b706SShreshtha Kumar Sahu
2589b2da53fSShreshtha Kumar Sahu if (drvdata->brightness)
2599b2da53fSShreshtha Kumar Sahu brightness = drvdata->brightness;
2609b2da53fSShreshtha Kumar Sahu else
26199ad98d2SKim, Milo brightness = drvdata->brightness = pdata->brt_val;
262b1e6b706SShreshtha Kumar Sahu
263bbe64532SKim, Milo if (brightness > drvdata->led_dev.max_brightness)
264bbe64532SKim, Milo brightness = drvdata->led_dev.max_brightness;
265bbe64532SKim, Milo
266b1e6b706SShreshtha Kumar Sahu reg_val[0] = gen_config; /* LM3530_GEN_CONFIG */
2676335f8faSKim, Milo reg_val[1] = als.config; /* LM3530_ALS_CONFIG */
268b1e6b706SShreshtha Kumar Sahu reg_val[2] = brt_ramp; /* LM3530_BRT_RAMP_RATE */
2696335f8faSKim, Milo reg_val[3] = als.imp_sel; /* LM3530_ALS_IMP_SELECT */
2708544d0e3SKim, Milo reg_val[4] = brightness; /* LM3530_BRT_CTRL_REG */
2716335f8faSKim, Milo reg_val[5] = als.zones[0]; /* LM3530_ALS_ZB0_REG */
2726335f8faSKim, Milo reg_val[6] = als.zones[1]; /* LM3530_ALS_ZB1_REG */
2736335f8faSKim, Milo reg_val[7] = als.zones[2]; /* LM3530_ALS_ZB2_REG */
2746335f8faSKim, Milo reg_val[8] = als.zones[3]; /* LM3530_ALS_ZB3_REG */
2758544d0e3SKim, Milo reg_val[9] = LM3530_DEF_ZT_0; /* LM3530_ALS_Z0T_REG */
2768544d0e3SKim, Milo reg_val[10] = LM3530_DEF_ZT_1; /* LM3530_ALS_Z1T_REG */
2778544d0e3SKim, Milo reg_val[11] = LM3530_DEF_ZT_2; /* LM3530_ALS_Z2T_REG */
2788544d0e3SKim, Milo reg_val[12] = LM3530_DEF_ZT_3; /* LM3530_ALS_Z3T_REG */
2798544d0e3SKim, Milo reg_val[13] = LM3530_DEF_ZT_4; /* LM3530_ALS_Z4T_REG */
280b1e6b706SShreshtha Kumar Sahu
2815e3b7c6bSAxel Lin ret = lm3530_led_enable(drvdata);
2825e3b7c6bSAxel Lin if (ret)
2839b2da53fSShreshtha Kumar Sahu return ret;
2849b2da53fSShreshtha Kumar Sahu
285b1e6b706SShreshtha Kumar Sahu for (i = 0; i < LM3530_REG_MAX; i++) {
286bb982009SKim, Milo /* do not update brightness register when pwm mode */
287bb982009SKim, Milo if (lm3530_reg[i] == LM3530_BRT_CTRL_REG &&
288bb982009SKim, Milo drvdata->mode == LM3530_BL_MODE_PWM) {
289bb982009SKim, Milo if (pwm->pwm_set_intensity)
290bb982009SKim, Milo pwm->pwm_set_intensity(reg_val[i],
291bb982009SKim, Milo drvdata->led_dev.max_brightness);
292bb982009SKim, Milo continue;
293bb982009SKim, Milo }
294bb982009SKim, Milo
295b1e6b706SShreshtha Kumar Sahu ret = i2c_smbus_write_byte_data(client,
296b1e6b706SShreshtha Kumar Sahu lm3530_reg[i], reg_val[i]);
297b1e6b706SShreshtha Kumar Sahu if (ret)
298b1e6b706SShreshtha Kumar Sahu break;
299b1e6b706SShreshtha Kumar Sahu }
300b1e6b706SShreshtha Kumar Sahu
301b1e6b706SShreshtha Kumar Sahu return ret;
302b1e6b706SShreshtha Kumar Sahu }
303b1e6b706SShreshtha Kumar Sahu
lm3530_brightness_set(struct led_classdev * led_cdev,enum led_brightness brt_val)304b1e6b706SShreshtha Kumar Sahu static void lm3530_brightness_set(struct led_classdev *led_cdev,
305b1e6b706SShreshtha Kumar Sahu enum led_brightness brt_val)
306b1e6b706SShreshtha Kumar Sahu {
307b1e6b706SShreshtha Kumar Sahu int err;
308b1e6b706SShreshtha Kumar Sahu struct lm3530_data *drvdata =
309b1e6b706SShreshtha Kumar Sahu container_of(led_cdev, struct lm3530_data, led_dev);
310bb982009SKim, Milo struct lm3530_platform_data *pdata = drvdata->pdata;
311bb982009SKim, Milo struct lm3530_pwm_data *pwm = &pdata->pwm_data;
312bb982009SKim, Milo u8 max_brightness = led_cdev->max_brightness;
313b1e6b706SShreshtha Kumar Sahu
314b1e6b706SShreshtha Kumar Sahu switch (drvdata->mode) {
315b1e6b706SShreshtha Kumar Sahu case LM3530_BL_MODE_MANUAL:
316b1e6b706SShreshtha Kumar Sahu
3179b2da53fSShreshtha Kumar Sahu if (!drvdata->enable) {
3189b2da53fSShreshtha Kumar Sahu err = lm3530_init_registers(drvdata);
3199b2da53fSShreshtha Kumar Sahu if (err) {
3209b2da53fSShreshtha Kumar Sahu dev_err(&drvdata->client->dev,
3219b2da53fSShreshtha Kumar Sahu "Register Init failed: %d\n", err);
3229b2da53fSShreshtha Kumar Sahu break;
3239b2da53fSShreshtha Kumar Sahu }
3249b2da53fSShreshtha Kumar Sahu }
3259b2da53fSShreshtha Kumar Sahu
326b1e6b706SShreshtha Kumar Sahu /* set the brightness in brightness control register*/
327b1e6b706SShreshtha Kumar Sahu err = i2c_smbus_write_byte_data(drvdata->client,
328bbe64532SKim, Milo LM3530_BRT_CTRL_REG, brt_val);
329b1e6b706SShreshtha Kumar Sahu if (err)
330b1e6b706SShreshtha Kumar Sahu dev_err(&drvdata->client->dev,
331b1e6b706SShreshtha Kumar Sahu "Unable to set brightness: %d\n", err);
3329b2da53fSShreshtha Kumar Sahu else
333bbe64532SKim, Milo drvdata->brightness = brt_val;
3349b2da53fSShreshtha Kumar Sahu
3355e3b7c6bSAxel Lin if (brt_val == 0)
3365e3b7c6bSAxel Lin lm3530_led_disable(drvdata);
337b1e6b706SShreshtha Kumar Sahu break;
338b1e6b706SShreshtha Kumar Sahu case LM3530_BL_MODE_ALS:
339b1e6b706SShreshtha Kumar Sahu break;
340b1e6b706SShreshtha Kumar Sahu case LM3530_BL_MODE_PWM:
341bb982009SKim, Milo if (pwm->pwm_set_intensity)
342bb982009SKim, Milo pwm->pwm_set_intensity(brt_val, max_brightness);
343b1e6b706SShreshtha Kumar Sahu break;
344b1e6b706SShreshtha Kumar Sahu default:
345b1e6b706SShreshtha Kumar Sahu break;
346b1e6b706SShreshtha Kumar Sahu }
347b1e6b706SShreshtha Kumar Sahu }
348b1e6b706SShreshtha Kumar Sahu
mode_show(struct device * dev,struct device_attribute * attr,char * buf)3495ccfa39dSDwaipayan Ray static ssize_t mode_show(struct device *dev,
35040b1445bSShreshtha Kumar Sahu struct device_attribute *attr, char *buf)
35140b1445bSShreshtha Kumar Sahu {
352e13d9786SKim, Milo struct led_classdev *led_cdev = dev_get_drvdata(dev);
353e13d9786SKim, Milo struct lm3530_data *drvdata;
35440b1445bSShreshtha Kumar Sahu int i, len = 0;
35540b1445bSShreshtha Kumar Sahu
356e13d9786SKim, Milo drvdata = container_of(led_cdev, struct lm3530_data, led_dev);
35740b1445bSShreshtha Kumar Sahu for (i = 0; i < ARRAY_SIZE(mode_map); i++)
35840b1445bSShreshtha Kumar Sahu if (drvdata->mode == mode_map[i].mode_val)
35940b1445bSShreshtha Kumar Sahu len += sprintf(buf + len, "[%s] ", mode_map[i].mode);
36040b1445bSShreshtha Kumar Sahu else
36140b1445bSShreshtha Kumar Sahu len += sprintf(buf + len, "%s ", mode_map[i].mode);
36240b1445bSShreshtha Kumar Sahu
36340b1445bSShreshtha Kumar Sahu len += sprintf(buf + len, "\n");
36440b1445bSShreshtha Kumar Sahu
36540b1445bSShreshtha Kumar Sahu return len;
36640b1445bSShreshtha Kumar Sahu }
367b1e6b706SShreshtha Kumar Sahu
mode_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)3685ccfa39dSDwaipayan Ray static ssize_t mode_store(struct device *dev, struct device_attribute
369b1e6b706SShreshtha Kumar Sahu *attr, const char *buf, size_t size)
370b1e6b706SShreshtha Kumar Sahu {
371e13d9786SKim, Milo struct led_classdev *led_cdev = dev_get_drvdata(dev);
372e13d9786SKim, Milo struct lm3530_data *drvdata;
373bb982009SKim, Milo struct lm3530_pwm_data *pwm;
374bb982009SKim, Milo u8 max_brightness;
375e13d9786SKim, Milo int mode, err;
376b1e6b706SShreshtha Kumar Sahu
377e13d9786SKim, Milo drvdata = container_of(led_cdev, struct lm3530_data, led_dev);
378bb982009SKim, Milo pwm = &drvdata->pdata->pwm_data;
379bb982009SKim, Milo max_brightness = led_cdev->max_brightness;
380b1e6b706SShreshtha Kumar Sahu mode = lm3530_get_mode_from_str(buf);
381b1e6b706SShreshtha Kumar Sahu if (mode < 0) {
382b1e6b706SShreshtha Kumar Sahu dev_err(dev, "Invalid mode\n");
383e0e02160SSachin Kamat return mode;
384b1e6b706SShreshtha Kumar Sahu }
385b1e6b706SShreshtha Kumar Sahu
386bb982009SKim, Milo drvdata->mode = mode;
387bb982009SKim, Milo
388bb982009SKim, Milo /* set pwm to low if unnecessary */
389bb982009SKim, Milo if (mode != LM3530_BL_MODE_PWM && pwm->pwm_set_intensity)
390bb982009SKim, Milo pwm->pwm_set_intensity(0, max_brightness);
391b1e6b706SShreshtha Kumar Sahu
392b1e6b706SShreshtha Kumar Sahu err = lm3530_init_registers(drvdata);
393b1e6b706SShreshtha Kumar Sahu if (err) {
394b1e6b706SShreshtha Kumar Sahu dev_err(dev, "Setting %s Mode failed :%d\n", buf, err);
395b1e6b706SShreshtha Kumar Sahu return err;
396b1e6b706SShreshtha Kumar Sahu }
397b1e6b706SShreshtha Kumar Sahu
398b1e6b706SShreshtha Kumar Sahu return sizeof(drvdata->mode);
399b1e6b706SShreshtha Kumar Sahu }
4005ccfa39dSDwaipayan Ray static DEVICE_ATTR_RW(mode);
401b1e6b706SShreshtha Kumar Sahu
40274c85039SJohan Hovold static struct attribute *lm3530_attrs[] = {
40374c85039SJohan Hovold &dev_attr_mode.attr,
40474c85039SJohan Hovold NULL
40574c85039SJohan Hovold };
40674c85039SJohan Hovold ATTRIBUTE_GROUPS(lm3530);
40774c85039SJohan Hovold
lm3530_probe(struct i2c_client * client)4085d080668SUwe Kleine-König static int lm3530_probe(struct i2c_client *client)
409b1e6b706SShreshtha Kumar Sahu {
41087aae1eaSJingoo Han struct lm3530_platform_data *pdata = dev_get_platdata(&client->dev);
411b1e6b706SShreshtha Kumar Sahu struct lm3530_data *drvdata;
412b1e6b706SShreshtha Kumar Sahu int err = 0;
413b1e6b706SShreshtha Kumar Sahu
414b1e6b706SShreshtha Kumar Sahu if (pdata == NULL) {
415b1e6b706SShreshtha Kumar Sahu dev_err(&client->dev, "platform data required\n");
4160b873f3dSBryan Wu return -ENODEV;
417b1e6b706SShreshtha Kumar Sahu }
418b1e6b706SShreshtha Kumar Sahu
419b1e6b706SShreshtha Kumar Sahu /* BL mode */
420b1e6b706SShreshtha Kumar Sahu if (pdata->mode > LM3530_BL_MODE_PWM) {
421b1e6b706SShreshtha Kumar Sahu dev_err(&client->dev, "Illegal Mode request\n");
4220b873f3dSBryan Wu return -EINVAL;
423b1e6b706SShreshtha Kumar Sahu }
424b1e6b706SShreshtha Kumar Sahu
425b1e6b706SShreshtha Kumar Sahu if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
426b1e6b706SShreshtha Kumar Sahu dev_err(&client->dev, "I2C_FUNC_I2C not supported\n");
4270b873f3dSBryan Wu return -EIO;
428b1e6b706SShreshtha Kumar Sahu }
429b1e6b706SShreshtha Kumar Sahu
4300b873f3dSBryan Wu drvdata = devm_kzalloc(&client->dev, sizeof(struct lm3530_data),
4310b873f3dSBryan Wu GFP_KERNEL);
4320b873f3dSBryan Wu if (drvdata == NULL)
4330b873f3dSBryan Wu return -ENOMEM;
434b1e6b706SShreshtha Kumar Sahu
435b1e6b706SShreshtha Kumar Sahu drvdata->mode = pdata->mode;
436b1e6b706SShreshtha Kumar Sahu drvdata->client = client;
437b1e6b706SShreshtha Kumar Sahu drvdata->pdata = pdata;
4389b2da53fSShreshtha Kumar Sahu drvdata->brightness = LED_OFF;
4399b2da53fSShreshtha Kumar Sahu drvdata->enable = false;
440b1e6b706SShreshtha Kumar Sahu drvdata->led_dev.name = LM3530_LED_DEV;
441b1e6b706SShreshtha Kumar Sahu drvdata->led_dev.brightness_set = lm3530_brightness_set;
442bbe64532SKim, Milo drvdata->led_dev.max_brightness = MAX_BRIGHTNESS;
44374c85039SJohan Hovold drvdata->led_dev.groups = lm3530_groups;
444b1e6b706SShreshtha Kumar Sahu
445b1e6b706SShreshtha Kumar Sahu i2c_set_clientdata(client, drvdata);
446b1e6b706SShreshtha Kumar Sahu
447a47df97cSSachin Kamat drvdata->regulator = devm_regulator_get(&client->dev, "vin");
4489b2da53fSShreshtha Kumar Sahu if (IS_ERR(drvdata->regulator)) {
4499b2da53fSShreshtha Kumar Sahu dev_err(&client->dev, "regulator get failed\n");
4509b2da53fSShreshtha Kumar Sahu err = PTR_ERR(drvdata->regulator);
4519b2da53fSShreshtha Kumar Sahu drvdata->regulator = NULL;
4520b873f3dSBryan Wu return err;
4539b2da53fSShreshtha Kumar Sahu }
4549b2da53fSShreshtha Kumar Sahu
4559b2da53fSShreshtha Kumar Sahu if (drvdata->pdata->brt_val) {
456b1e6b706SShreshtha Kumar Sahu err = lm3530_init_registers(drvdata);
457b1e6b706SShreshtha Kumar Sahu if (err < 0) {
4589b2da53fSShreshtha Kumar Sahu dev_err(&client->dev,
4599b2da53fSShreshtha Kumar Sahu "Register Init failed: %d\n", err);
460e0e02160SSachin Kamat return err;
461b1e6b706SShreshtha Kumar Sahu }
4629b2da53fSShreshtha Kumar Sahu }
4639b2da53fSShreshtha Kumar Sahu err = led_classdev_register(&client->dev, &drvdata->led_dev);
464b1e6b706SShreshtha Kumar Sahu if (err < 0) {
465b1e6b706SShreshtha Kumar Sahu dev_err(&client->dev, "Register led class failed: %d\n", err);
466e0e02160SSachin Kamat return err;
467b1e6b706SShreshtha Kumar Sahu }
468b1e6b706SShreshtha Kumar Sahu
469b1e6b706SShreshtha Kumar Sahu return 0;
470b1e6b706SShreshtha Kumar Sahu }
471b1e6b706SShreshtha Kumar Sahu
lm3530_remove(struct i2c_client * client)472ed5c2f5fSUwe Kleine-König static void lm3530_remove(struct i2c_client *client)
473b1e6b706SShreshtha Kumar Sahu {
474b1e6b706SShreshtha Kumar Sahu struct lm3530_data *drvdata = i2c_get_clientdata(client);
475b1e6b706SShreshtha Kumar Sahu
4765e3b7c6bSAxel Lin lm3530_led_disable(drvdata);
477b1e6b706SShreshtha Kumar Sahu led_classdev_unregister(&drvdata->led_dev);
478b1e6b706SShreshtha Kumar Sahu }
479b1e6b706SShreshtha Kumar Sahu
480b1e6b706SShreshtha Kumar Sahu static const struct i2c_device_id lm3530_id[] = {
481b1e6b706SShreshtha Kumar Sahu {LM3530_NAME, 0},
482b1e6b706SShreshtha Kumar Sahu {}
483b1e6b706SShreshtha Kumar Sahu };
484d5f33d45SAxel Lin MODULE_DEVICE_TABLE(i2c, lm3530_id);
485b1e6b706SShreshtha Kumar Sahu
486b1e6b706SShreshtha Kumar Sahu static struct i2c_driver lm3530_i2c_driver = {
487*d9ff8a8eSUwe Kleine-König .probe = lm3530_probe,
488df07cf81SBill Pemberton .remove = lm3530_remove,
489b1e6b706SShreshtha Kumar Sahu .id_table = lm3530_id,
490b1e6b706SShreshtha Kumar Sahu .driver = {
491b1e6b706SShreshtha Kumar Sahu .name = LM3530_NAME,
492b1e6b706SShreshtha Kumar Sahu },
493b1e6b706SShreshtha Kumar Sahu };
494b1e6b706SShreshtha Kumar Sahu
49509a0d183SAxel Lin module_i2c_driver(lm3530_i2c_driver);
496b1e6b706SShreshtha Kumar Sahu
497b1e6b706SShreshtha Kumar Sahu MODULE_DESCRIPTION("Back Light driver for LM3530");
498b1e6b706SShreshtha Kumar Sahu MODULE_LICENSE("GPL v2");
499b1e6b706SShreshtha Kumar Sahu MODULE_AUTHOR("Shreshtha Kumar SAHU <shreshthakumar.sahu@stericsson.com>");
500