123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563 |
- /*
- * LED driver for Maxim MAX77803 - leds-max77803.c
- *
- * Copyright (C) 2013 Samsung co. Ltd.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation.
- */
- #include <linux/kernel.h>
- #include <linux/init.h>
- #include <linux/platform_device.h>
- #include <linux/leds.h>
- #include <linux/workqueue.h>
- #include <linux/spinlock.h>
- #include <linux/slab.h>
- #include <linux/mfd/max77803.h>
- #include <linux/mfd/max77803-private.h>
- #include <linux/leds-max77803.h>
- #include <linux/ctype.h>
- #include <linux/gpio.h>
- #ifdef CONFIG_LEDS_SWITCH
- #include <linux/gpio.h>
- #define FLASH_SWITCH_REMOVED_REVISION 0x05
- #endif
- #undef DEBUG_FLASH
- //#define DEBUG_FLASH
- #if defined(DEBUG_FLASH)
- #define DEBUG_MAX77803(fmt, args...) pr_err(fmt, ##args)
- #else
- #define DEBUG_MAX77803(fmt, args...) do{}while(0)
- #endif
- struct max77803_led_data {
- struct led_classdev led;
- struct max77803_dev *max77803;
- struct max77803_led *data;
- struct i2c_client *i2c;
- struct work_struct work;
- struct mutex lock;
- spinlock_t value_lock;
- int brightness;
- int test_brightness;
- #ifdef CONFIG_LEDS_SEPERATE_MOVIE_FLASH
- int movie_brightness;
- #endif
- };
- static u8 led_en_mask[MAX77803_LED_MAX] = {
- MAX77803_FLASH_FLED1_EN,
- MAX77803_TORCH_FLED1_EN,
- };
- static u8 led_en_shift[MAX77803_LED_MAX] = {
- 6,
- 2,
- };
- static u8 reg_led_timer[MAX77803_LED_MAX] = {
- MAX77803_LED_REG_FLASH_TIMER,
- MAX77803_LED_REG_ITORCHTORCHTIMER,
- };
- static u8 reg_led_current[MAX77803_LED_MAX] = {
- MAX77803_LED_REG_IFLASH,
- MAX77803_LED_REG_ITORCH,
- };
- static u8 led_current_mask[MAX77803_LED_MAX] = {
- MAX77803_FLASH_IOUT,
- MAX77803_TORCH_IOUT,
- };
- static u8 led_current_shift[MAX77803_LED_MAX] = {
- 0,
- 0,
- };
- extern struct class *camera_class; /*sys/class/camera*/
- struct device *flash_dev;
- static int flash_torch_en;
- extern int led_torch_en;
- extern int led_flash_en;
- static int max77803_set_bits(struct i2c_client *client, const u8 reg,
- const u8 mask, const u8 inval)
- {
- int ret;
- u8 value;
- ret = max77803_read_reg(client, reg, &value);
- if (unlikely(ret < 0))
- return ret;
- value = (value & ~mask) | (inval & mask);
- ret = max77803_write_reg(client, reg, value);
- return ret;
- }
- #ifdef CONFIG_LEDS_SEPERATE_MOVIE_FLASH
- static ssize_t max77803_show_movie_brightness(struct device *dev,
- struct device_attribute *devattr, char *buf)
- {
- struct led_classdev *led_cdev = dev_get_drvdata(dev);
- struct max77803_led_data *led_data = container_of(led_cdev, struct max77803_led_data, led);
- return sprintf(buf, "%d\n", led_data->movie_brightness);
- }
- static ssize_t max77803_store_movie_brightness(struct device *dev,
- struct device_attribute *attr, const char *buf, size_t size)
- {
- struct led_classdev *led_cdev = dev_get_drvdata(dev);
- struct max77803_led_data *led_data = container_of(led_cdev, struct max77803_led_data, led);
- unsigned long movie_brightness = simple_strtoul(buf, NULL, 0);
- int set_brightness, ret;
- u8 shift = led_current_shift[led_data->data->id];
- if (movie_brightness > 0) {
- led_data->movie_brightness = (int)movie_brightness;
- set_brightness = led_data->movie_brightness;
- } else {
- led_data->movie_brightness = 0;
- set_brightness = led_data->brightness;
- }
- pr_debug("[LED] %s:movie_brightness = %d\n", __func__, led_data->movie_brightness);
- ret = max77803_set_bits(led_data->i2c, reg_led_current[led_data->data->id],
- led_current_mask[led_data->data->id],
- set_brightness << shift);
- if (unlikely(ret))
- pr_err("%s: can't set led level %d\n", __func__, ret);
- return size;
- }
- static DEVICE_ATTR(movie_brightness, S_IWUSR|S_IWGRP|S_IRUGO,
- max77803_show_movie_brightness, max77803_store_movie_brightness);
- #endif
- /*
- static void print_all_reg_value(struct i2c_client *client)
- {
- u8 value;
- u8 i;
- for (i = 0; i != 0x11; ++i) {
- max77803_read_reg(client, i, &value);
- printk(KERN_ERR "LEDS_MAX77803 REG(%d) = %x\n", i, value);
- }
- }
- */
- static int max77803_led_get_en_value(struct max77803_led_data *led_data, int on)
- {
- int mode;
- int ret;
- if (on)
- mode = 0x03; /*triggered via serial interface*/
- else {
- if (reg_led_timer[led_data->data->id]
- == MAX77803_LED_REG_FLASH_TIMER)
- mode = 0x01; /*Flash triggered via FLASHEN*/
- else
- mode = 0x02; /*Torch triggered via TORCHEN*/
- if (flash_torch_en) {
- if (mode == 0x01) {
- ret = gpio_request(led_flash_en, "max77803_flash_en");
- if (ret)
- pr_err("can't get max77803_flash_en");
- else {
- DEBUG_MAX77803("[LED] %s: max77803_flash_en set 0\n", __func__);
- gpio_direction_output(led_flash_en, 0);
- gpio_free(led_flash_en);
- }
- }
- if (mode == 0x02) {
- ret = gpio_request(led_torch_en, "max77803_torch_en");
- if (ret)
- pr_err("can't get max77803_torch_en");
- else {
- DEBUG_MAX77803("[LED] %s: max77803_torch_en set 0\n", __func__);
- gpio_direction_output(led_torch_en, 0);
- gpio_free(led_torch_en);
- }
- }
- } else
- pr_err("%s : can't find gpio", __func__);
- }
- DEBUG_MAX77803("[LED] %s: led mode: 0x%x, on: %d\n", __func__, mode, on);
- return mode;
- }
- int max77803_led_en(int onoff, int mode)
- {
- int ret = 0;
- if (flash_torch_en) {
- if (onoff) { /* enable */
- if (mode) { /* flash */
- DEBUG_MAX77803("[LED] %s: max77803_flash_en set 1\n", __func__);
- gpio_direction_output(led_flash_en, 1);
- } else { /* torch */
- DEBUG_MAX77803("[LED] %s: max77803_torch_en set 1\n", __func__);
- gpio_direction_output(led_torch_en, 1);
- }
- } else { /* disable */
- if (mode) { /* flash */
- DEBUG_MAX77803("[LED] %s: max77803_flash_en set 0\n", __func__);
- gpio_direction_output(led_flash_en, 0);
- } else { /* torch */
- DEBUG_MAX77803("[LED] %s: max77803_torch_en set 0\n", __func__);
- gpio_direction_output(led_torch_en, 0);
- }
- }
- } else {
- pr_err("%s : Error!!, find gpio", __func__);
- ret = -EINVAL;
- }
- return ret;
- }
- EXPORT_SYMBOL(max77803_led_en);
- static void max77803_led_set(struct led_classdev *led_cdev,
- enum led_brightness value)
- {
- unsigned long flags;
- struct max77803_led_data *led_data
- = container_of(led_cdev, struct max77803_led_data, led);
- DEBUG_MAX77803("[LED] %s\n", __func__);
- spin_lock_irqsave(&led_data->value_lock, flags);
- led_data->test_brightness = min((int)value, MAX77803_FLASH_IOUT);
- spin_unlock_irqrestore(&led_data->value_lock, flags);
- schedule_work(&led_data->work);
- }
- static void led_set(struct max77803_led_data *led_data)
- {
- int ret;
- struct max77803_led *data = led_data->data;
- int id = data->id;
- u8 shift = led_current_shift[id];
- int value;
- if (led_data->test_brightness == LED_OFF) {
- DEBUG_MAX77803("[LED] %s : LED_OFF\n", __func__);
- value = max77803_led_get_en_value(led_data, 0);
- ret = max77803_set_bits(led_data->i2c,
- MAX77803_LED_REG_FLASH_EN,
- led_en_mask[id],
- value << led_en_shift[id]);
- if (unlikely(ret))
- goto error_set_bits;
- ret = max77803_set_bits(led_data->i2c, reg_led_current[id],
- led_current_mask[id],
- led_data->brightness << shift);
- if (unlikely(ret))
- goto error_set_bits;
- return;
- }
- /* Set current */
- ret = max77803_set_bits(led_data->i2c, reg_led_current[id],
- led_current_mask[id],
- led_data->test_brightness << shift);
- if (unlikely(ret))
- goto error_set_bits;
- /* Turn on LED */
- DEBUG_MAX77803("[LED] %s : LED turns on\n", __func__);
- value = max77803_led_get_en_value(led_data, 1);
- ret = max77803_set_bits(led_data->i2c, MAX77803_LED_REG_FLASH_EN,
- led_en_mask[id],
- value << led_en_shift[id]);
- if (unlikely(ret))
- goto error_set_bits;
- return;
- error_set_bits:
- pr_err("%s: can't set led level %d\n", __func__, ret);
- return;
- }
- static void max77803_led_work(struct work_struct *work)
- {
- struct max77803_led_data *led_data
- = container_of(work, struct max77803_led_data, work);
- DEBUG_MAX77803("[LED] %s\n", __func__);
- mutex_lock(&led_data->lock);
- led_set(led_data);
- mutex_unlock(&led_data->lock);
- }
- static int max77803_led_setup(struct max77803_led_data *led_data)
- {
- int ret = 0;
- struct max77803_led *data = led_data->data;
- int id = data->id;
- int value;
- DEBUG_MAX77803("[LED] %s\n", __func__);
- ret |= max77803_write_reg(led_data->i2c, MAX77803_LED_REG_VOUT_CNTL,
- MAX77803_BOOST_FLASH_MODE_FLED1);
- ret |= max77803_write_reg(led_data->i2c, MAX77803_LED_REG_VOUT_FLASH,
- MAX77803_BOOST_VOUT_FLASH_FROM_VOLT(3300));
- ret |= max77803_write_reg(led_data->i2c, MAX77803_CHG_REG_CHG_CNFG_11, 0x2B);
- ret |= max77803_write_reg(led_data->i2c,
- MAX77803_LED_REG_MAX_FLASH1, 0xBC);
- ret |= max77803_write_reg(led_data->i2c,
- MAX77803_LED_REG_MAX_FLASH2, 0x00);
- value = max77803_led_get_en_value(led_data, 0);
- ret |= max77803_set_bits(led_data->i2c, MAX77803_LED_REG_FLASH_EN,
- led_en_mask[id],
- value << led_en_shift[id]);
- /* Set TORCH_TMR_DUR or FLASH_TMR_DUR */
- if (reg_led_timer[id] == MAX77803_LED_REG_FLASH_TIMER) {
- ret |= max77803_write_reg(led_data->i2c, reg_led_timer[id],
- (data->timer | data->timer_mode << 7));
- } else {
- ret |= max77803_write_reg(led_data->i2c, reg_led_timer[id],
- 0xC0);
- }
- /* Set current */
- ret |= max77803_set_bits(led_data->i2c, reg_led_current[id],
- led_current_mask[id],
- led_data->brightness << led_current_shift[id]);
- return ret;
- }
- static ssize_t max77803_flash(struct device *dev,
- struct device_attribute *attr, const char *buf, size_t size)
- {
- struct led_classdev *led_cdev = dev_get_drvdata(dev);
- ssize_t ret = -EINVAL;
- char *after;
- unsigned long state = simple_strtoul(buf, &after, 10);
- size_t count = after - buf;
- DEBUG_MAX77803("[LED] %s\n", __func__);
- if (isspace(*after))
- count++;
- if (count == size) {
- ret = count;
- if (state > led_cdev->max_brightness)
- state = led_cdev->max_brightness;
- led_cdev->brightness = state;
- pr_warn("[LED] %s : led_cdev->brightness %d\n",
- __func__, led_cdev->brightness);
- if (!(led_cdev->flags & LED_SUSPENDED))
- led_cdev->brightness_set(led_cdev, state);
- }
- return ret;
- }
- static DEVICE_ATTR(rear_flash, S_IWUSR|S_IWGRP|S_IROTH,
- NULL, max77803_flash);
- static int max77803_led_probe(struct platform_device *pdev)
- {
- int ret = 0;
- int i;
- struct max77803_dev *max77803 = dev_get_drvdata(pdev->dev.parent);
- struct max77803_platform_data *max77803_pdata
- = dev_get_platdata(max77803->dev);
- struct max77803_led_platform_data *pdata = max77803_pdata->led_data;
- struct max77803_led_data *led_data;
- struct max77803_led *data;
- struct max77803_led_data **led_datas;
- if (pdata == NULL) {
- pr_err("[LED] no platform data for this led is found\n");
- return -EFAULT;
- }
- led_datas = kzalloc(sizeof(struct max77803_led_data *)
- * MAX77803_LED_MAX, GFP_KERNEL);
- if (unlikely(!led_datas)) {
- pr_err("[LED] memory allocation error %s", __func__);
- return -ENOMEM;
- }
- platform_set_drvdata(pdev, led_datas);
- if (led_torch_en && led_flash_en) {
- DEBUG_MAX77803("[LED] %s, led_torch %d, led_flash %d\n", __func__,
- led_torch_en, led_flash_en);
- flash_torch_en = 1;
- } else {
- pr_err("%s : can't find gpio", __func__);
- flash_torch_en = 0;
- }
- for (i = 0; i != pdata->num_leds; ++i) {
- data = &(pdata->leds[i]);
- led_data = kzalloc(sizeof(struct max77803_led_data),
- GFP_KERNEL);
- led_datas[i] = led_data;
- if (unlikely(!led_data)) {
- pr_err("[LED] memory allocation error %s\n", __func__);
- ret = -ENOMEM;
- continue;
- }
- led_data->max77803 = max77803;
- led_data->i2c = max77803->i2c;
- led_data->data = data;
- led_data->led.name = data->name;
- led_data->led.brightness_set = max77803_led_set;
- led_data->led.brightness = LED_OFF;
- led_data->brightness = data->brightness;
- led_data->led.flags = 0;
- led_data->led.max_brightness = reg_led_timer[data->id] == MAX77803_LED_REG_FLASH_TIMER
- ? MAX_FLASH_DRV_LEVEL : MAX_TORCH_DRV_LEVEL;
- mutex_init(&led_data->lock);
- spin_lock_init(&led_data->value_lock);
- INIT_WORK(&led_data->work, max77803_led_work);
- ret = led_classdev_register(&pdev->dev, &led_data->led);
- if (unlikely(ret)) {
- pr_err("unable to register LED\n");
- kfree(led_data);
- ret = -EFAULT;
- continue;
- }
- ret = max77803_led_setup(led_data);
- if (unlikely(ret)) {
- pr_err("unable to register LED\n");
- mutex_destroy(&led_data->lock);
- led_classdev_unregister(&led_data->led);
- kfree(led_data);
- ret = -EFAULT;
- }
- }
- /* print_all_reg_value(max77803->i2c); */
- if (!IS_ERR(camera_class)) {
- flash_dev = device_create(camera_class, NULL, 0, led_datas[1], "flash");
- if (flash_dev < 0)
- pr_err("Failed to create device(flash)!\n");
- if (device_create_file(flash_dev, &dev_attr_rear_flash) < 0) {
- pr_err("failed to create device file, %s\n",
- dev_attr_rear_flash.attr.name);
- }
- #ifdef CONFIG_LEDS_SEPERATE_MOVIE_FLASH
- if (device_create_file(flash_dev, &dev_attr_movie_brightness) < 0) {
- pr_err("failed to create device file, %s\n",
- dev_attr_movie_brightness.attr.name);
- }
- #endif
- } else
- pr_err("Failed to create device(flash) because of nothing camera class!\n");
- #ifdef CONFIG_LEDS_SWITCH
- if (system_rev < FLASH_SWITCH_REMOVED_REVISION) {
- if (gpio_request(GPIO_CAM_SW_EN, "CAM_SW_EN"))
- pr_err("failed to request CAM_SW_EN\n");
- else
- gpio_direction_output(GPIO_CAM_SW_EN, 1);
- }
- #endif
- return ret;
- }
- static int __devexit max77803_led_remove(struct platform_device *pdev)
- {
- struct max77803_led_data **led_datas = platform_get_drvdata(pdev);
- int i;
- for (i = 0; i != MAX77803_LED_MAX; ++i) {
- if (led_datas[i] == NULL)
- continue;
- cancel_work_sync(&led_datas[i]->work);
- mutex_destroy(&led_datas[i]->lock);
- led_classdev_unregister(&led_datas[i]->led);
- kfree(led_datas[i]);
- }
- kfree(led_datas);
- device_remove_file(flash_dev, &dev_attr_rear_flash);
- #ifdef CONFIG_LEDS_SEPERATE_MOVIE_FLASH
- device_remove_file(flash_dev, &dev_attr_movie_brightness);
- #endif
- device_destroy(camera_class, 0);
- class_destroy(camera_class);
- return 0;
- }
- void max77803_led_shutdown(struct device *dev)
- {
- struct max77803_led_data **led_datas = dev_get_drvdata(dev);
- /* Turn off LED */
- max77803_set_bits(led_datas[1]->i2c,
- MAX77803_LED_REG_FLASH_EN,
- led_en_mask[1],
- 0x02 << led_en_shift[1]);
- }
- static struct platform_driver max77803_led_driver = {
- .probe = max77803_led_probe,
- .remove = __devexit_p(max77803_led_remove),
- .driver = {
- .name = "max77803-led",
- .owner = THIS_MODULE,
- .shutdown = max77803_led_shutdown,
- },
- };
- static int __init max77803_led_init(void)
- {
- return platform_driver_register(&max77803_led_driver);
- }
- module_init(max77803_led_init);
- static void __exit max77803_led_exit(void)
- {
- platform_driver_unregister(&max77803_led_driver);
- }
- module_exit(max77803_led_exit);
- MODULE_AUTHOR("Samsung co. Ltd.");
- MODULE_DESCRIPTION("MAX77803 LED driver");
- MODULE_LICENSE("GPL");
|