Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 ST-Ericsson SA. |
| 3 | * Copyright (C) 2009 Motorola, Inc. |
| 4 | * |
| 5 | * License Terms: GNU General Public License v2 |
| 6 | * |
| 7 | * Simple driver for National Semiconductor LM3530 Backlight driver chip |
| 8 | * |
| 9 | * Author: Shreshtha Kumar SAHU <shreshthakumar.sahu@stericsson.com> |
| 10 | * based on leds-lm3530.c by Dan Murphy <D.Murphy@motorola.com> |
| 11 | */ |
| 12 | |
| 13 | #include <linux/i2c.h> |
| 14 | #include <linux/leds.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/input.h> |
| 18 | #include <linux/led-lm3530.h> |
| 19 | #include <linux/types.h> |
Shreshtha Kumar Sahu | 9b2da53 | 2011-05-24 17:13:28 -0700 | [diff] [blame] | 20 | #include <linux/regulator/consumer.h> |
Paul Gortmaker | 54f4ded | 2011-07-03 13:56:03 -0400 | [diff] [blame] | 21 | #include <linux/module.h> |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 22 | |
| 23 | #define LM3530_LED_DEV "lcd-backlight" |
| 24 | #define LM3530_NAME "lm3530-led" |
| 25 | |
| 26 | #define LM3530_GEN_CONFIG 0x10 |
| 27 | #define LM3530_ALS_CONFIG 0x20 |
| 28 | #define LM3530_BRT_RAMP_RATE 0x30 |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 29 | #define LM3530_ALS_IMP_SELECT 0x41 |
| 30 | #define LM3530_BRT_CTRL_REG 0xA0 |
| 31 | #define LM3530_ALS_ZB0_REG 0x60 |
| 32 | #define LM3530_ALS_ZB1_REG 0x61 |
| 33 | #define LM3530_ALS_ZB2_REG 0x62 |
| 34 | #define LM3530_ALS_ZB3_REG 0x63 |
| 35 | #define LM3530_ALS_Z0T_REG 0x70 |
| 36 | #define LM3530_ALS_Z1T_REG 0x71 |
| 37 | #define LM3530_ALS_Z2T_REG 0x72 |
| 38 | #define LM3530_ALS_Z3T_REG 0x73 |
| 39 | #define LM3530_ALS_Z4T_REG 0x74 |
Kim, Milo | 8544d0e | 2012-03-23 15:02:12 -0700 | [diff] [blame] | 40 | #define LM3530_REG_MAX 14 |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 41 | |
| 42 | /* General Control Register */ |
| 43 | #define LM3530_EN_I2C_SHIFT (0) |
| 44 | #define LM3530_RAMP_LAW_SHIFT (1) |
| 45 | #define LM3530_MAX_CURR_SHIFT (2) |
| 46 | #define LM3530_EN_PWM_SHIFT (5) |
| 47 | #define LM3530_PWM_POL_SHIFT (6) |
| 48 | #define LM3530_EN_PWM_SIMPLE_SHIFT (7) |
| 49 | |
| 50 | #define LM3530_ENABLE_I2C (1 << LM3530_EN_I2C_SHIFT) |
| 51 | #define LM3530_ENABLE_PWM (1 << LM3530_EN_PWM_SHIFT) |
| 52 | #define LM3530_POL_LOW (1 << LM3530_PWM_POL_SHIFT) |
| 53 | #define LM3530_ENABLE_PWM_SIMPLE (1 << LM3530_EN_PWM_SIMPLE_SHIFT) |
| 54 | |
| 55 | /* ALS Config Register Options */ |
| 56 | #define LM3530_ALS_AVG_TIME_SHIFT (0) |
| 57 | #define LM3530_EN_ALS_SHIFT (3) |
| 58 | #define LM3530_ALS_SEL_SHIFT (5) |
| 59 | |
| 60 | #define LM3530_ENABLE_ALS (3 << LM3530_EN_ALS_SHIFT) |
| 61 | |
| 62 | /* Brightness Ramp Rate Register */ |
| 63 | #define LM3530_BRT_RAMP_FALL_SHIFT (0) |
| 64 | #define LM3530_BRT_RAMP_RISE_SHIFT (3) |
| 65 | |
| 66 | /* ALS Resistor Select */ |
| 67 | #define LM3530_ALS1_IMP_SHIFT (0) |
| 68 | #define LM3530_ALS2_IMP_SHIFT (4) |
| 69 | |
| 70 | /* Zone Boundary Register defaults */ |
Shreshtha Kumar Sahu | 40b1445 | 2011-07-25 17:13:17 -0700 | [diff] [blame] | 71 | #define LM3530_ALS_ZB_MAX (4) |
| 72 | #define LM3530_ALS_WINDOW_mV (1000) |
| 73 | #define LM3530_ALS_OFFSET_mV (4) |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 74 | |
| 75 | /* Zone Target Register defaults */ |
Shreshtha Kumar Sahu | 40b1445 | 2011-07-25 17:13:17 -0700 | [diff] [blame] | 76 | #define LM3530_DEF_ZT_0 (0x7F) |
| 77 | #define LM3530_DEF_ZT_1 (0x66) |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 78 | #define LM3530_DEF_ZT_2 (0x4C) |
Shreshtha Kumar Sahu | 40b1445 | 2011-07-25 17:13:17 -0700 | [diff] [blame] | 79 | #define LM3530_DEF_ZT_3 (0x33) |
| 80 | #define LM3530_DEF_ZT_4 (0x19) |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 81 | |
Kim, Milo | bbe6453 | 2012-03-23 15:02:11 -0700 | [diff] [blame] | 82 | /* 7 bits are used for the brightness : LM3530_BRT_CTRL_REG */ |
| 83 | #define MAX_BRIGHTNESS (127) |
| 84 | |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 85 | struct lm3530_mode_map { |
| 86 | const char *mode; |
| 87 | enum lm3530_mode mode_val; |
| 88 | }; |
| 89 | |
| 90 | static struct lm3530_mode_map mode_map[] = { |
| 91 | { "man", LM3530_BL_MODE_MANUAL }, |
| 92 | { "als", LM3530_BL_MODE_ALS }, |
| 93 | { "pwm", LM3530_BL_MODE_PWM }, |
| 94 | }; |
| 95 | |
| 96 | /** |
| 97 | * struct lm3530_data |
| 98 | * @led_dev: led class device |
| 99 | * @client: i2c client |
| 100 | * @pdata: LM3530 platform data |
| 101 | * @mode: mode of operation - manual, ALS, PWM |
Shreshtha Kumar Sahu | 9b2da53 | 2011-05-24 17:13:28 -0700 | [diff] [blame] | 102 | * @regulator: regulator |
| 103 | * @brighness: previous brightness value |
| 104 | * @enable: regulator is enabled |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 105 | */ |
| 106 | struct lm3530_data { |
| 107 | struct led_classdev led_dev; |
| 108 | struct i2c_client *client; |
| 109 | struct lm3530_platform_data *pdata; |
| 110 | enum lm3530_mode mode; |
Shreshtha Kumar Sahu | 9b2da53 | 2011-05-24 17:13:28 -0700 | [diff] [blame] | 111 | struct regulator *regulator; |
| 112 | enum led_brightness brightness; |
| 113 | bool enable; |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | static const u8 lm3530_reg[LM3530_REG_MAX] = { |
| 117 | LM3530_GEN_CONFIG, |
| 118 | LM3530_ALS_CONFIG, |
| 119 | LM3530_BRT_RAMP_RATE, |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 120 | LM3530_ALS_IMP_SELECT, |
| 121 | LM3530_BRT_CTRL_REG, |
| 122 | LM3530_ALS_ZB0_REG, |
| 123 | LM3530_ALS_ZB1_REG, |
| 124 | LM3530_ALS_ZB2_REG, |
| 125 | LM3530_ALS_ZB3_REG, |
| 126 | LM3530_ALS_Z0T_REG, |
| 127 | LM3530_ALS_Z1T_REG, |
| 128 | LM3530_ALS_Z2T_REG, |
| 129 | LM3530_ALS_Z3T_REG, |
| 130 | LM3530_ALS_Z4T_REG, |
| 131 | }; |
| 132 | |
| 133 | static int lm3530_get_mode_from_str(const char *str) |
| 134 | { |
| 135 | int i; |
| 136 | |
| 137 | for (i = 0; i < ARRAY_SIZE(mode_map); i++) |
| 138 | if (sysfs_streq(str, mode_map[i].mode)) |
| 139 | return mode_map[i].mode_val; |
| 140 | |
| 141 | return -1; |
| 142 | } |
| 143 | |
| 144 | static int lm3530_init_registers(struct lm3530_data *drvdata) |
| 145 | { |
| 146 | int ret = 0; |
| 147 | int i; |
| 148 | u8 gen_config; |
| 149 | u8 als_config = 0; |
| 150 | u8 brt_ramp; |
| 151 | u8 als_imp_sel = 0; |
| 152 | u8 brightness; |
| 153 | u8 reg_val[LM3530_REG_MAX]; |
Shreshtha Kumar Sahu | 40b1445 | 2011-07-25 17:13:17 -0700 | [diff] [blame] | 154 | u8 zones[LM3530_ALS_ZB_MAX]; |
| 155 | u32 als_vmin, als_vmax, als_vstep; |
Kim, Milo | 99ad98d | 2012-03-23 15:02:13 -0700 | [diff] [blame^] | 156 | struct lm3530_platform_data *pdata = drvdata->pdata; |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 157 | struct i2c_client *client = drvdata->client; |
Kim, Milo | 99ad98d | 2012-03-23 15:02:13 -0700 | [diff] [blame^] | 158 | struct lm3530_pwm_data *pwm = &pdata->pwm_data; |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 159 | |
Kim, Milo | 99ad98d | 2012-03-23 15:02:13 -0700 | [diff] [blame^] | 160 | gen_config = (pdata->brt_ramp_law << LM3530_RAMP_LAW_SHIFT) | |
| 161 | ((pdata->max_current & 7) << LM3530_MAX_CURR_SHIFT); |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 162 | |
| 163 | if (drvdata->mode == LM3530_BL_MODE_MANUAL || |
| 164 | drvdata->mode == LM3530_BL_MODE_ALS) |
| 165 | gen_config |= (LM3530_ENABLE_I2C); |
| 166 | |
| 167 | if (drvdata->mode == LM3530_BL_MODE_ALS) { |
Kim, Milo | 99ad98d | 2012-03-23 15:02:13 -0700 | [diff] [blame^] | 168 | if (pdata->als_vmax == 0) { |
| 169 | pdata->als_vmin = 0; |
| 170 | pdata->als_vmax = LM3530_ALS_WINDOW_mV; |
Shreshtha Kumar Sahu | 40b1445 | 2011-07-25 17:13:17 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Kim, Milo | 99ad98d | 2012-03-23 15:02:13 -0700 | [diff] [blame^] | 173 | als_vmin = pdata->als_vmin; |
| 174 | als_vmax = pdata->als_vmax; |
Shreshtha Kumar Sahu | 40b1445 | 2011-07-25 17:13:17 -0700 | [diff] [blame] | 175 | |
| 176 | if ((als_vmax - als_vmin) > LM3530_ALS_WINDOW_mV) |
Kim, Milo | 99ad98d | 2012-03-23 15:02:13 -0700 | [diff] [blame^] | 177 | pdata->als_vmax = als_vmax = |
Shreshtha Kumar Sahu | 40b1445 | 2011-07-25 17:13:17 -0700 | [diff] [blame] | 178 | als_vmin + LM3530_ALS_WINDOW_mV; |
| 179 | |
| 180 | /* n zone boundary makes n+1 zones */ |
| 181 | als_vstep = (als_vmax - als_vmin) / (LM3530_ALS_ZB_MAX + 1); |
| 182 | |
| 183 | for (i = 0; i < LM3530_ALS_ZB_MAX; i++) |
| 184 | zones[i] = (((als_vmin + LM3530_ALS_OFFSET_mV) + |
| 185 | als_vstep + (i * als_vstep)) * LED_FULL) |
| 186 | / 1000; |
| 187 | |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 188 | als_config = |
Kim, Milo | 99ad98d | 2012-03-23 15:02:13 -0700 | [diff] [blame^] | 189 | (pdata->als_avrg_time << LM3530_ALS_AVG_TIME_SHIFT) | |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 190 | (LM3530_ENABLE_ALS) | |
Kim, Milo | 99ad98d | 2012-03-23 15:02:13 -0700 | [diff] [blame^] | 191 | (pdata->als_input_mode << LM3530_ALS_SEL_SHIFT); |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 192 | |
| 193 | als_imp_sel = |
Kim, Milo | 99ad98d | 2012-03-23 15:02:13 -0700 | [diff] [blame^] | 194 | (pdata->als1_resistor_sel << LM3530_ALS1_IMP_SHIFT) | |
| 195 | (pdata->als2_resistor_sel << LM3530_ALS2_IMP_SHIFT); |
Shreshtha Kumar Sahu | 40b1445 | 2011-07-25 17:13:17 -0700 | [diff] [blame] | 196 | |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | if (drvdata->mode == LM3530_BL_MODE_PWM) |
| 200 | gen_config |= (LM3530_ENABLE_PWM) | |
Kim, Milo | 99ad98d | 2012-03-23 15:02:13 -0700 | [diff] [blame^] | 201 | (pdata->pwm_pol_hi << LM3530_PWM_POL_SHIFT) | |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 202 | (LM3530_ENABLE_PWM_SIMPLE); |
| 203 | |
Kim, Milo | 99ad98d | 2012-03-23 15:02:13 -0700 | [diff] [blame^] | 204 | brt_ramp = (pdata->brt_ramp_fall << LM3530_BRT_RAMP_FALL_SHIFT) | |
| 205 | (pdata->brt_ramp_rise << LM3530_BRT_RAMP_RISE_SHIFT); |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 206 | |
Shreshtha Kumar Sahu | 9b2da53 | 2011-05-24 17:13:28 -0700 | [diff] [blame] | 207 | if (drvdata->brightness) |
| 208 | brightness = drvdata->brightness; |
| 209 | else |
Kim, Milo | 99ad98d | 2012-03-23 15:02:13 -0700 | [diff] [blame^] | 210 | brightness = drvdata->brightness = pdata->brt_val; |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 211 | |
Kim, Milo | bbe6453 | 2012-03-23 15:02:11 -0700 | [diff] [blame] | 212 | if (brightness > drvdata->led_dev.max_brightness) |
| 213 | brightness = drvdata->led_dev.max_brightness; |
| 214 | |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 215 | reg_val[0] = gen_config; /* LM3530_GEN_CONFIG */ |
| 216 | reg_val[1] = als_config; /* LM3530_ALS_CONFIG */ |
| 217 | reg_val[2] = brt_ramp; /* LM3530_BRT_RAMP_RATE */ |
Kim, Milo | 8544d0e | 2012-03-23 15:02:12 -0700 | [diff] [blame] | 218 | reg_val[3] = als_imp_sel; /* LM3530_ALS_IMP_SELECT */ |
| 219 | reg_val[4] = brightness; /* LM3530_BRT_CTRL_REG */ |
| 220 | reg_val[5] = zones[0]; /* LM3530_ALS_ZB0_REG */ |
| 221 | reg_val[6] = zones[1]; /* LM3530_ALS_ZB1_REG */ |
| 222 | reg_val[7] = zones[2]; /* LM3530_ALS_ZB2_REG */ |
| 223 | reg_val[8] = zones[3]; /* LM3530_ALS_ZB3_REG */ |
| 224 | reg_val[9] = LM3530_DEF_ZT_0; /* LM3530_ALS_Z0T_REG */ |
| 225 | reg_val[10] = LM3530_DEF_ZT_1; /* LM3530_ALS_Z1T_REG */ |
| 226 | reg_val[11] = LM3530_DEF_ZT_2; /* LM3530_ALS_Z2T_REG */ |
| 227 | reg_val[12] = LM3530_DEF_ZT_3; /* LM3530_ALS_Z3T_REG */ |
| 228 | reg_val[13] = LM3530_DEF_ZT_4; /* LM3530_ALS_Z4T_REG */ |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 229 | |
Shreshtha Kumar Sahu | 9b2da53 | 2011-05-24 17:13:28 -0700 | [diff] [blame] | 230 | if (!drvdata->enable) { |
| 231 | ret = regulator_enable(drvdata->regulator); |
| 232 | if (ret) { |
| 233 | dev_err(&drvdata->client->dev, |
| 234 | "Enable regulator failed\n"); |
| 235 | return ret; |
| 236 | } |
| 237 | drvdata->enable = true; |
| 238 | } |
| 239 | |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 240 | for (i = 0; i < LM3530_REG_MAX; i++) { |
Kim, Milo | bb98200 | 2012-03-23 15:02:12 -0700 | [diff] [blame] | 241 | /* do not update brightness register when pwm mode */ |
| 242 | if (lm3530_reg[i] == LM3530_BRT_CTRL_REG && |
| 243 | drvdata->mode == LM3530_BL_MODE_PWM) { |
| 244 | if (pwm->pwm_set_intensity) |
| 245 | pwm->pwm_set_intensity(reg_val[i], |
| 246 | drvdata->led_dev.max_brightness); |
| 247 | continue; |
| 248 | } |
| 249 | |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 250 | ret = i2c_smbus_write_byte_data(client, |
| 251 | lm3530_reg[i], reg_val[i]); |
| 252 | if (ret) |
| 253 | break; |
| 254 | } |
| 255 | |
| 256 | return ret; |
| 257 | } |
| 258 | |
| 259 | static void lm3530_brightness_set(struct led_classdev *led_cdev, |
| 260 | enum led_brightness brt_val) |
| 261 | { |
| 262 | int err; |
| 263 | struct lm3530_data *drvdata = |
| 264 | container_of(led_cdev, struct lm3530_data, led_dev); |
Kim, Milo | bb98200 | 2012-03-23 15:02:12 -0700 | [diff] [blame] | 265 | struct lm3530_platform_data *pdata = drvdata->pdata; |
| 266 | struct lm3530_pwm_data *pwm = &pdata->pwm_data; |
| 267 | u8 max_brightness = led_cdev->max_brightness; |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 268 | |
| 269 | switch (drvdata->mode) { |
| 270 | case LM3530_BL_MODE_MANUAL: |
| 271 | |
Shreshtha Kumar Sahu | 9b2da53 | 2011-05-24 17:13:28 -0700 | [diff] [blame] | 272 | if (!drvdata->enable) { |
| 273 | err = lm3530_init_registers(drvdata); |
| 274 | if (err) { |
| 275 | dev_err(&drvdata->client->dev, |
| 276 | "Register Init failed: %d\n", err); |
| 277 | break; |
| 278 | } |
| 279 | } |
| 280 | |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 281 | /* set the brightness in brightness control register*/ |
| 282 | err = i2c_smbus_write_byte_data(drvdata->client, |
Kim, Milo | bbe6453 | 2012-03-23 15:02:11 -0700 | [diff] [blame] | 283 | LM3530_BRT_CTRL_REG, brt_val); |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 284 | if (err) |
| 285 | dev_err(&drvdata->client->dev, |
| 286 | "Unable to set brightness: %d\n", err); |
Shreshtha Kumar Sahu | 9b2da53 | 2011-05-24 17:13:28 -0700 | [diff] [blame] | 287 | else |
Kim, Milo | bbe6453 | 2012-03-23 15:02:11 -0700 | [diff] [blame] | 288 | drvdata->brightness = brt_val; |
Shreshtha Kumar Sahu | 9b2da53 | 2011-05-24 17:13:28 -0700 | [diff] [blame] | 289 | |
| 290 | if (brt_val == 0) { |
| 291 | err = regulator_disable(drvdata->regulator); |
| 292 | if (err) |
| 293 | dev_err(&drvdata->client->dev, |
| 294 | "Disable regulator failed\n"); |
| 295 | drvdata->enable = false; |
| 296 | } |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 297 | break; |
| 298 | case LM3530_BL_MODE_ALS: |
| 299 | break; |
| 300 | case LM3530_BL_MODE_PWM: |
Kim, Milo | bb98200 | 2012-03-23 15:02:12 -0700 | [diff] [blame] | 301 | if (pwm->pwm_set_intensity) |
| 302 | pwm->pwm_set_intensity(brt_val, max_brightness); |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 303 | break; |
| 304 | default: |
| 305 | break; |
| 306 | } |
| 307 | } |
| 308 | |
Shreshtha Kumar Sahu | 40b1445 | 2011-07-25 17:13:17 -0700 | [diff] [blame] | 309 | static ssize_t lm3530_mode_get(struct device *dev, |
| 310 | struct device_attribute *attr, char *buf) |
| 311 | { |
Kim, Milo | e13d978 | 2012-03-23 15:02:11 -0700 | [diff] [blame] | 312 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 313 | struct lm3530_data *drvdata; |
Shreshtha Kumar Sahu | 40b1445 | 2011-07-25 17:13:17 -0700 | [diff] [blame] | 314 | int i, len = 0; |
| 315 | |
Kim, Milo | e13d978 | 2012-03-23 15:02:11 -0700 | [diff] [blame] | 316 | drvdata = container_of(led_cdev, struct lm3530_data, led_dev); |
Shreshtha Kumar Sahu | 40b1445 | 2011-07-25 17:13:17 -0700 | [diff] [blame] | 317 | for (i = 0; i < ARRAY_SIZE(mode_map); i++) |
| 318 | if (drvdata->mode == mode_map[i].mode_val) |
| 319 | len += sprintf(buf + len, "[%s] ", mode_map[i].mode); |
| 320 | else |
| 321 | len += sprintf(buf + len, "%s ", mode_map[i].mode); |
| 322 | |
| 323 | len += sprintf(buf + len, "\n"); |
| 324 | |
| 325 | return len; |
| 326 | } |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 327 | |
| 328 | static ssize_t lm3530_mode_set(struct device *dev, struct device_attribute |
| 329 | *attr, const char *buf, size_t size) |
| 330 | { |
Kim, Milo | e13d978 | 2012-03-23 15:02:11 -0700 | [diff] [blame] | 331 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 332 | struct lm3530_data *drvdata; |
Kim, Milo | bb98200 | 2012-03-23 15:02:12 -0700 | [diff] [blame] | 333 | struct lm3530_pwm_data *pwm; |
| 334 | u8 max_brightness; |
Kim, Milo | e13d978 | 2012-03-23 15:02:11 -0700 | [diff] [blame] | 335 | int mode, err; |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 336 | |
Kim, Milo | e13d978 | 2012-03-23 15:02:11 -0700 | [diff] [blame] | 337 | drvdata = container_of(led_cdev, struct lm3530_data, led_dev); |
Kim, Milo | bb98200 | 2012-03-23 15:02:12 -0700 | [diff] [blame] | 338 | pwm = &drvdata->pdata->pwm_data; |
| 339 | max_brightness = led_cdev->max_brightness; |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 340 | mode = lm3530_get_mode_from_str(buf); |
| 341 | if (mode < 0) { |
| 342 | dev_err(dev, "Invalid mode\n"); |
| 343 | return -EINVAL; |
| 344 | } |
| 345 | |
Kim, Milo | bb98200 | 2012-03-23 15:02:12 -0700 | [diff] [blame] | 346 | drvdata->mode = mode; |
| 347 | |
| 348 | /* set pwm to low if unnecessary */ |
| 349 | if (mode != LM3530_BL_MODE_PWM && pwm->pwm_set_intensity) |
| 350 | pwm->pwm_set_intensity(0, max_brightness); |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 351 | |
| 352 | err = lm3530_init_registers(drvdata); |
| 353 | if (err) { |
| 354 | dev_err(dev, "Setting %s Mode failed :%d\n", buf, err); |
| 355 | return err; |
| 356 | } |
| 357 | |
| 358 | return sizeof(drvdata->mode); |
| 359 | } |
Shreshtha Kumar Sahu | 40b1445 | 2011-07-25 17:13:17 -0700 | [diff] [blame] | 360 | static DEVICE_ATTR(mode, 0644, lm3530_mode_get, lm3530_mode_set); |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 361 | |
| 362 | static int __devinit lm3530_probe(struct i2c_client *client, |
| 363 | const struct i2c_device_id *id) |
| 364 | { |
| 365 | struct lm3530_platform_data *pdata = client->dev.platform_data; |
| 366 | struct lm3530_data *drvdata; |
| 367 | int err = 0; |
| 368 | |
| 369 | if (pdata == NULL) { |
| 370 | dev_err(&client->dev, "platform data required\n"); |
| 371 | err = -ENODEV; |
| 372 | goto err_out; |
| 373 | } |
| 374 | |
| 375 | /* BL mode */ |
| 376 | if (pdata->mode > LM3530_BL_MODE_PWM) { |
| 377 | dev_err(&client->dev, "Illegal Mode request\n"); |
| 378 | err = -EINVAL; |
| 379 | goto err_out; |
| 380 | } |
| 381 | |
| 382 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { |
| 383 | dev_err(&client->dev, "I2C_FUNC_I2C not supported\n"); |
| 384 | err = -EIO; |
| 385 | goto err_out; |
| 386 | } |
| 387 | |
| 388 | drvdata = kzalloc(sizeof(struct lm3530_data), GFP_KERNEL); |
| 389 | if (drvdata == NULL) { |
| 390 | err = -ENOMEM; |
| 391 | goto err_out; |
| 392 | } |
| 393 | |
| 394 | drvdata->mode = pdata->mode; |
| 395 | drvdata->client = client; |
| 396 | drvdata->pdata = pdata; |
Shreshtha Kumar Sahu | 9b2da53 | 2011-05-24 17:13:28 -0700 | [diff] [blame] | 397 | drvdata->brightness = LED_OFF; |
| 398 | drvdata->enable = false; |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 399 | drvdata->led_dev.name = LM3530_LED_DEV; |
| 400 | drvdata->led_dev.brightness_set = lm3530_brightness_set; |
Kim, Milo | bbe6453 | 2012-03-23 15:02:11 -0700 | [diff] [blame] | 401 | drvdata->led_dev.max_brightness = MAX_BRIGHTNESS; |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 402 | |
| 403 | i2c_set_clientdata(client, drvdata); |
| 404 | |
Shreshtha Kumar Sahu | 9b2da53 | 2011-05-24 17:13:28 -0700 | [diff] [blame] | 405 | drvdata->regulator = regulator_get(&client->dev, "vin"); |
| 406 | if (IS_ERR(drvdata->regulator)) { |
| 407 | dev_err(&client->dev, "regulator get failed\n"); |
| 408 | err = PTR_ERR(drvdata->regulator); |
| 409 | drvdata->regulator = NULL; |
| 410 | goto err_regulator_get; |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Shreshtha Kumar Sahu | 9b2da53 | 2011-05-24 17:13:28 -0700 | [diff] [blame] | 413 | if (drvdata->pdata->brt_val) { |
| 414 | err = lm3530_init_registers(drvdata); |
| 415 | if (err < 0) { |
| 416 | dev_err(&client->dev, |
| 417 | "Register Init failed: %d\n", err); |
| 418 | err = -ENODEV; |
| 419 | goto err_reg_init; |
| 420 | } |
| 421 | } |
| 422 | err = led_classdev_register(&client->dev, &drvdata->led_dev); |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 423 | if (err < 0) { |
| 424 | dev_err(&client->dev, "Register led class failed: %d\n", err); |
| 425 | err = -ENODEV; |
| 426 | goto err_class_register; |
| 427 | } |
| 428 | |
| 429 | err = device_create_file(drvdata->led_dev.dev, &dev_attr_mode); |
| 430 | if (err < 0) { |
| 431 | dev_err(&client->dev, "File device creation failed: %d\n", err); |
| 432 | err = -ENODEV; |
| 433 | goto err_create_file; |
| 434 | } |
| 435 | |
| 436 | return 0; |
| 437 | |
| 438 | err_create_file: |
| 439 | led_classdev_unregister(&drvdata->led_dev); |
| 440 | err_class_register: |
| 441 | err_reg_init: |
Shreshtha Kumar Sahu | 9b2da53 | 2011-05-24 17:13:28 -0700 | [diff] [blame] | 442 | regulator_put(drvdata->regulator); |
| 443 | err_regulator_get: |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 444 | kfree(drvdata); |
| 445 | err_out: |
| 446 | return err; |
| 447 | } |
| 448 | |
| 449 | static int __devexit lm3530_remove(struct i2c_client *client) |
| 450 | { |
| 451 | struct lm3530_data *drvdata = i2c_get_clientdata(client); |
| 452 | |
| 453 | device_remove_file(drvdata->led_dev.dev, &dev_attr_mode); |
Shreshtha Kumar Sahu | 9b2da53 | 2011-05-24 17:13:28 -0700 | [diff] [blame] | 454 | |
| 455 | if (drvdata->enable) |
| 456 | regulator_disable(drvdata->regulator); |
| 457 | regulator_put(drvdata->regulator); |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 458 | led_classdev_unregister(&drvdata->led_dev); |
| 459 | kfree(drvdata); |
| 460 | return 0; |
| 461 | } |
| 462 | |
| 463 | static const struct i2c_device_id lm3530_id[] = { |
| 464 | {LM3530_NAME, 0}, |
| 465 | {} |
| 466 | }; |
Axel Lin | d5f33d4 | 2011-05-17 15:44:09 -0700 | [diff] [blame] | 467 | MODULE_DEVICE_TABLE(i2c, lm3530_id); |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 468 | |
| 469 | static struct i2c_driver lm3530_i2c_driver = { |
| 470 | .probe = lm3530_probe, |
Axel Lin | 93f5398 | 2011-10-31 17:12:15 -0700 | [diff] [blame] | 471 | .remove = __devexit_p(lm3530_remove), |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 472 | .id_table = lm3530_id, |
| 473 | .driver = { |
| 474 | .name = LM3530_NAME, |
| 475 | .owner = THIS_MODULE, |
| 476 | }, |
| 477 | }; |
| 478 | |
Axel Lin | 09a0d18 | 2012-01-10 15:09:27 -0800 | [diff] [blame] | 479 | module_i2c_driver(lm3530_i2c_driver); |
Shreshtha Kumar Sahu | b1e6b70 | 2011-03-22 16:30:16 -0700 | [diff] [blame] | 480 | |
| 481 | MODULE_DESCRIPTION("Back Light driver for LM3530"); |
| 482 | MODULE_LICENSE("GPL v2"); |
| 483 | MODULE_AUTHOR("Shreshtha Kumar SAHU <shreshthakumar.sahu@stericsson.com>"); |