1 /* 2 * Simple driver for Texas Instruments LM3642 LED Flash driver chip 3 * Copyright (C) 2012 Texas Instruments 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License version 2 as 7 * published by the Free Software Foundation. 8 * 9 */ 10 #include <linux/module.h> 11 #include <linux/delay.h> 12 #include <linux/i2c.h> 13 #include <linux/leds.h> 14 #include <linux/slab.h> 15 #include <linux/platform_device.h> 16 #include <linux/fs.h> 17 #include <linux/regmap.h> 18 #include <linux/platform_data/leds-lm3642.h> 19 20 #define REG_FILT_TIME (0x0) 21 #define REG_IVFM_MODE (0x1) 22 #define REG_TORCH_TIME (0x6) 23 #define REG_FLASH (0x8) 24 #define REG_I_CTRL (0x9) 25 #define REG_ENABLE (0xA) 26 #define REG_FLAG (0xB) 27 #define REG_MAX (0xB) 28 29 #define UVLO_EN_SHIFT (7) 30 #define IVM_D_TH_SHIFT (2) 31 #define TORCH_RAMP_UP_TIME_SHIFT (3) 32 #define TORCH_RAMP_DN_TIME_SHIFT (0) 33 #define INDUCTOR_I_LIMIT_SHIFT (6) 34 #define FLASH_RAMP_TIME_SHIFT (3) 35 #define FLASH_TOUT_TIME_SHIFT (0) 36 #define TORCH_I_SHIFT (4) 37 #define FLASH_I_SHIFT (0) 38 #define IVFM_SHIFT (7) 39 #define TX_PIN_EN_SHIFT (6) 40 #define STROBE_PIN_EN_SHIFT (5) 41 #define TORCH_PIN_EN_SHIFT (4) 42 #define MODE_BITS_SHIFT (0) 43 44 #define UVLO_EN_MASK (0x1) 45 #define IVM_D_TH_MASK (0x7) 46 #define TORCH_RAMP_UP_TIME_MASK (0x7) 47 #define TORCH_RAMP_DN_TIME_MASK (0x7) 48 #define INDUCTOR_I_LIMIT_MASK (0x1) 49 #define FLASH_RAMP_TIME_MASK (0x7) 50 #define FLASH_TOUT_TIME_MASK (0x7) 51 #define TORCH_I_MASK (0x7) 52 #define FLASH_I_MASK (0xF) 53 #define IVFM_MASK (0x1) 54 #define TX_PIN_EN_MASK (0x1) 55 #define STROBE_PIN_EN_MASK (0x1) 56 #define TORCH_PIN_EN_MASK (0x1) 57 #define MODE_BITS_MASK (0x73) 58 #define EX_PIN_CONTROL_MASK (0x71) 59 #define EX_PIN_ENABLE_MASK (0x70) 60 61 enum lm3642_mode { 62 MODES_STASNDBY = 0, 63 MODES_INDIC, 64 MODES_TORCH, 65 MODES_FLASH 66 }; 67 68 struct lm3642_chip_data { 69 struct device *dev; 70 71 struct led_classdev cdev_flash; 72 struct led_classdev cdev_torch; 73 struct led_classdev cdev_indicator; 74 75 u8 br_flash; 76 u8 br_torch; 77 u8 br_indicator; 78 79 enum lm3642_torch_pin_enable torch_pin; 80 enum lm3642_strobe_pin_enable strobe_pin; 81 enum lm3642_tx_pin_enable tx_pin; 82 83 struct lm3642_platform_data *pdata; 84 struct regmap *regmap; 85 struct mutex lock; 86 87 unsigned int last_flag; 88 }; 89 90 /* chip initialize */ 91 static int lm3642_chip_init(struct lm3642_chip_data *chip) 92 { 93 int ret; 94 struct lm3642_platform_data *pdata = chip->pdata; 95 96 /* set enable register */ 97 ret = regmap_update_bits(chip->regmap, REG_ENABLE, EX_PIN_ENABLE_MASK, 98 pdata->tx_pin); 99 if (ret < 0) 100 dev_err(chip->dev, "Failed to update REG_ENABLE Register\n"); 101 return ret; 102 } 103 104 /* chip control */ 105 static int lm3642_control(struct lm3642_chip_data *chip, 106 u8 brightness, enum lm3642_mode opmode) 107 { 108 int ret; 109 110 ret = regmap_read(chip->regmap, REG_FLAG, &chip->last_flag); 111 if (ret < 0) { 112 dev_err(chip->dev, "Failed to read REG_FLAG Register\n"); 113 goto out; 114 } 115 116 if (chip->last_flag) 117 dev_info(chip->dev, "Last FLAG is 0x%x\n", chip->last_flag); 118 119 /* brightness 0 means off-state */ 120 if (!brightness) 121 opmode = MODES_STASNDBY; 122 123 switch (opmode) { 124 case MODES_TORCH: 125 ret = regmap_update_bits(chip->regmap, REG_I_CTRL, 126 TORCH_I_MASK << TORCH_I_SHIFT, 127 (brightness - 1) << TORCH_I_SHIFT); 128 129 if (chip->torch_pin) 130 opmode |= (TORCH_PIN_EN_MASK << TORCH_PIN_EN_SHIFT); 131 break; 132 133 case MODES_FLASH: 134 ret = regmap_update_bits(chip->regmap, REG_I_CTRL, 135 FLASH_I_MASK << FLASH_I_SHIFT, 136 (brightness - 1) << FLASH_I_SHIFT); 137 138 if (chip->strobe_pin) 139 opmode |= (STROBE_PIN_EN_MASK << STROBE_PIN_EN_SHIFT); 140 break; 141 142 case MODES_INDIC: 143 ret = regmap_update_bits(chip->regmap, REG_I_CTRL, 144 TORCH_I_MASK << TORCH_I_SHIFT, 145 (brightness - 1) << TORCH_I_SHIFT); 146 break; 147 148 case MODES_STASNDBY: 149 150 break; 151 152 default: 153 return ret; 154 } 155 if (ret < 0) { 156 dev_err(chip->dev, "Failed to write REG_I_CTRL Register\n"); 157 goto out; 158 } 159 160 if (chip->tx_pin) 161 opmode |= (TX_PIN_EN_MASK << TX_PIN_EN_SHIFT); 162 163 ret = regmap_update_bits(chip->regmap, REG_ENABLE, 164 MODE_BITS_MASK << MODE_BITS_SHIFT, 165 opmode << MODE_BITS_SHIFT); 166 out: 167 return ret; 168 } 169 170 /* torch */ 171 172 /* torch pin config for lm3642*/ 173 static ssize_t lm3642_torch_pin_store(struct device *dev, 174 struct device_attribute *attr, 175 const char *buf, size_t size) 176 { 177 ssize_t ret; 178 struct led_classdev *led_cdev = dev_get_drvdata(dev); 179 struct lm3642_chip_data *chip = 180 container_of(led_cdev, struct lm3642_chip_data, cdev_indicator); 181 unsigned int state; 182 183 ret = kstrtouint(buf, 10, &state); 184 if (ret) 185 goto out_strtoint; 186 if (state != 0) 187 state = 0x01 << TORCH_PIN_EN_SHIFT; 188 189 chip->torch_pin = state; 190 ret = regmap_update_bits(chip->regmap, REG_ENABLE, 191 TORCH_PIN_EN_MASK << TORCH_PIN_EN_SHIFT, 192 state); 193 if (ret < 0) 194 goto out; 195 196 return size; 197 out: 198 dev_err(chip->dev, "%s:i2c access fail to register\n", __func__); 199 return ret; 200 out_strtoint: 201 dev_err(chip->dev, "%s: fail to change str to int\n", __func__); 202 return ret; 203 } 204 205 static DEVICE_ATTR(torch_pin, S_IWUSR, NULL, lm3642_torch_pin_store); 206 207 static int lm3642_torch_brightness_set(struct led_classdev *cdev, 208 enum led_brightness brightness) 209 { 210 struct lm3642_chip_data *chip = 211 container_of(cdev, struct lm3642_chip_data, cdev_torch); 212 int ret; 213 214 mutex_lock(&chip->lock); 215 chip->br_torch = brightness; 216 ret = lm3642_control(chip, chip->br_torch, MODES_TORCH); 217 mutex_unlock(&chip->lock); 218 return ret; 219 } 220 221 /* flash */ 222 223 /* strobe pin config for lm3642*/ 224 static ssize_t lm3642_strobe_pin_store(struct device *dev, 225 struct device_attribute *attr, 226 const char *buf, size_t size) 227 { 228 ssize_t ret; 229 struct led_classdev *led_cdev = dev_get_drvdata(dev); 230 struct lm3642_chip_data *chip = 231 container_of(led_cdev, struct lm3642_chip_data, cdev_indicator); 232 unsigned int state; 233 234 ret = kstrtouint(buf, 10, &state); 235 if (ret) 236 goto out_strtoint; 237 if (state != 0) 238 state = 0x01 << STROBE_PIN_EN_SHIFT; 239 240 chip->strobe_pin = state; 241 ret = regmap_update_bits(chip->regmap, REG_ENABLE, 242 STROBE_PIN_EN_MASK << STROBE_PIN_EN_SHIFT, 243 state); 244 if (ret < 0) 245 goto out; 246 247 return size; 248 out: 249 dev_err(chip->dev, "%s:i2c access fail to register\n", __func__); 250 return ret; 251 out_strtoint: 252 dev_err(chip->dev, "%s: fail to change str to int\n", __func__); 253 return ret; 254 } 255 256 static DEVICE_ATTR(strobe_pin, S_IWUSR, NULL, lm3642_strobe_pin_store); 257 258 static int lm3642_strobe_brightness_set(struct led_classdev *cdev, 259 enum led_brightness brightness) 260 { 261 struct lm3642_chip_data *chip = 262 container_of(cdev, struct lm3642_chip_data, cdev_flash); 263 int ret; 264 265 mutex_lock(&chip->lock); 266 chip->br_flash = brightness; 267 ret = lm3642_control(chip, chip->br_flash, MODES_FLASH); 268 mutex_unlock(&chip->lock); 269 return ret; 270 } 271 272 /* indicator */ 273 static int lm3642_indicator_brightness_set(struct led_classdev *cdev, 274 enum led_brightness brightness) 275 { 276 struct lm3642_chip_data *chip = 277 container_of(cdev, struct lm3642_chip_data, cdev_indicator); 278 int ret; 279 280 mutex_lock(&chip->lock); 281 chip->br_indicator = brightness; 282 ret = lm3642_control(chip, chip->br_indicator, MODES_INDIC); 283 mutex_unlock(&chip->lock); 284 return ret; 285 } 286 287 static const struct regmap_config lm3642_regmap = { 288 .reg_bits = 8, 289 .val_bits = 8, 290 .max_register = REG_MAX, 291 }; 292 293 static struct attribute *lm3642_flash_attrs[] = { 294 &dev_attr_strobe_pin.attr, 295 NULL 296 }; 297 ATTRIBUTE_GROUPS(lm3642_flash); 298 299 static struct attribute *lm3642_torch_attrs[] = { 300 &dev_attr_torch_pin.attr, 301 NULL 302 }; 303 ATTRIBUTE_GROUPS(lm3642_torch); 304 305 static int lm3642_probe(struct i2c_client *client, 306 const struct i2c_device_id *id) 307 { 308 struct lm3642_platform_data *pdata = dev_get_platdata(&client->dev); 309 struct lm3642_chip_data *chip; 310 311 int err; 312 313 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { 314 dev_err(&client->dev, "i2c functionality check fail.\n"); 315 return -EOPNOTSUPP; 316 } 317 318 if (pdata == NULL) { 319 dev_err(&client->dev, "needs Platform Data.\n"); 320 return -ENODATA; 321 } 322 323 chip = devm_kzalloc(&client->dev, 324 sizeof(struct lm3642_chip_data), GFP_KERNEL); 325 if (!chip) 326 return -ENOMEM; 327 328 chip->dev = &client->dev; 329 chip->pdata = pdata; 330 331 chip->tx_pin = pdata->tx_pin; 332 chip->torch_pin = pdata->torch_pin; 333 chip->strobe_pin = pdata->strobe_pin; 334 335 chip->regmap = devm_regmap_init_i2c(client, &lm3642_regmap); 336 if (IS_ERR(chip->regmap)) { 337 err = PTR_ERR(chip->regmap); 338 dev_err(&client->dev, "Failed to allocate register map: %d\n", 339 err); 340 return err; 341 } 342 343 mutex_init(&chip->lock); 344 i2c_set_clientdata(client, chip); 345 346 err = lm3642_chip_init(chip); 347 if (err < 0) 348 goto err_out; 349 350 /* flash */ 351 chip->cdev_flash.name = "flash"; 352 chip->cdev_flash.max_brightness = 16; 353 chip->cdev_flash.brightness_set_blocking = lm3642_strobe_brightness_set; 354 chip->cdev_flash.default_trigger = "flash"; 355 chip->cdev_flash.groups = lm3642_flash_groups, 356 err = led_classdev_register((struct device *) 357 &client->dev, &chip->cdev_flash); 358 if (err < 0) { 359 dev_err(chip->dev, "failed to register flash\n"); 360 goto err_out; 361 } 362 363 /* torch */ 364 chip->cdev_torch.name = "torch"; 365 chip->cdev_torch.max_brightness = 8; 366 chip->cdev_torch.brightness_set_blocking = lm3642_torch_brightness_set; 367 chip->cdev_torch.default_trigger = "torch"; 368 chip->cdev_torch.groups = lm3642_torch_groups, 369 err = led_classdev_register((struct device *) 370 &client->dev, &chip->cdev_torch); 371 if (err < 0) { 372 dev_err(chip->dev, "failed to register torch\n"); 373 goto err_create_torch_file; 374 } 375 376 /* indicator */ 377 chip->cdev_indicator.name = "indicator"; 378 chip->cdev_indicator.max_brightness = 8; 379 chip->cdev_indicator.brightness_set_blocking = 380 lm3642_indicator_brightness_set; 381 err = led_classdev_register((struct device *) 382 &client->dev, &chip->cdev_indicator); 383 if (err < 0) { 384 dev_err(chip->dev, "failed to register indicator\n"); 385 goto err_create_indicator_file; 386 } 387 388 dev_info(&client->dev, "LM3642 is initialized\n"); 389 return 0; 390 391 err_create_indicator_file: 392 led_classdev_unregister(&chip->cdev_torch); 393 err_create_torch_file: 394 led_classdev_unregister(&chip->cdev_flash); 395 err_out: 396 return err; 397 } 398 399 static int lm3642_remove(struct i2c_client *client) 400 { 401 struct lm3642_chip_data *chip = i2c_get_clientdata(client); 402 403 led_classdev_unregister(&chip->cdev_indicator); 404 led_classdev_unregister(&chip->cdev_torch); 405 led_classdev_unregister(&chip->cdev_flash); 406 regmap_write(chip->regmap, REG_ENABLE, 0); 407 return 0; 408 } 409 410 static const struct i2c_device_id lm3642_id[] = { 411 {LM3642_NAME, 0}, 412 {} 413 }; 414 415 MODULE_DEVICE_TABLE(i2c, lm3642_id); 416 417 static struct i2c_driver lm3642_i2c_driver = { 418 .driver = { 419 .name = LM3642_NAME, 420 .pm = NULL, 421 }, 422 .probe = lm3642_probe, 423 .remove = lm3642_remove, 424 .id_table = lm3642_id, 425 }; 426 427 module_i2c_driver(lm3642_i2c_driver); 428 429 MODULE_DESCRIPTION("Texas Instruments Flash Lighting driver for LM3642"); 430 MODULE_AUTHOR("Daniel Jeong <daniel.jeong@ti.com>"); 431 MODULE_AUTHOR("G.Shark Jeong <gshark.jeong@gmail.com>"); 432 MODULE_LICENSE("GPL v2"); 433