| /* |
| * leds-s2mu106.c - LED class driver for S2MU106 FLASH LEDs. |
| * |
| * Copyright (C) 2018 Samsung Electronics |
| * |
| * 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/module.h> |
| #include <linux/err.h> |
| #include <linux/slab.h> |
| #include <linux/workqueue.h> |
| #include <linux/gpio.h> |
| #include <linux/of_gpio.h> |
| #include <linux/power_supply.h> |
| #include <linux/mfd/samsung/s2mu106.h> |
| #include <linux/leds-s2mu106.h> |
| #include <linux/platform_device.h> |
| #include <linux/sec_batt.h> |
| |
| #define CONTROL_I2C 0 |
| #define CONTROL_GPIO 1 |
| |
| struct s2mu106_fled_data *g_fled_data; |
| |
| extern struct class *camera_class; |
| struct device *flash_dev; |
| |
| static char *s2mu106_fled_mode_string[] = { |
| "OFF", |
| "TORCH", |
| "FLASH", |
| }; |
| |
| static char *s2mu106_fled_operating_mode_string[] = { |
| "AUTO", |
| "BOOST", |
| "TA", |
| "SYS", |
| }; |
| |
| /*Channel values range fron 1 to 3 so 0 value is never obtained check function s2mu106_fled_set_torch_curr*/ |
| static int s2mu106_fled_torch_curr_max[] = { |
| -1, 320, 320, 320 |
| }; |
| |
| /*Channel values range fron 1 to 3 so 0 value is never obtained check function s2mu106_fled_set_flash_curr*/ |
| static int s2mu106_fled_flash_curr_max[] = { |
| -1, 1600, 1600, 500 |
| }; |
| |
| static void s2mu106_fled_test_read(struct s2mu106_fled_data *fled) |
| { |
| u8 data; |
| char str[1016] = {0,}; |
| int i; |
| struct i2c_client *i2c = fled->i2c; |
| |
| for (i = 0x0B; i <= 0x0C; i++) { |
| s2mu106_read_reg(i2c, i, &data); |
| sprintf(str+strlen(str), "0x%02x:0x%02x, ", i, data); |
| } |
| for (i = 0x14; i <= 0x15; i++) { |
| s2mu106_read_reg(i2c, i, &data); |
| sprintf(str+strlen(str), "0x%02x:0x%02x, ", i, data); |
| } |
| |
| for (i = 0x53; i <= 0x5A; i++) { |
| s2mu106_read_reg(i2c, i, &data); |
| sprintf(str+strlen(str), "0x%02x:0x%02x, ", i, data); |
| } |
| |
| s2mu106_read_reg(i2c, 0x5B, &data); |
| pr_err("%s: %s0x5B:0x%02x\n", __func__, str, data); |
| |
| memset(str,0,strlen(str)); |
| |
| for (i = 0x5C; i <= 0x62; i++) { |
| s2mu106_read_reg(i2c, i, &data); |
| sprintf(str+strlen(str), "0x%02x:0x%02x, ", i, data); |
| } |
| |
| s2mu106_read_reg(i2c, 0x63, &data); |
| sprintf(str+strlen(str), "0x63:0x%02x, ", data); |
| |
| s2mu106_read_reg(i2c, 0x66, &data); |
| sprintf(str+strlen(str), "0x66:0x%02x, ", data); |
| |
| s2mu106_read_reg(i2c, 0x67, &data); |
| pr_err("%s: %s0x67:0x%02x\n", __func__, str, data); |
| |
| } |
| |
| static int s2mu106_fled_get_flash_curr(struct s2mu106_fled_data *fled, int chan) |
| { |
| int curr = -1; |
| u8 data; |
| u8 dest; |
| |
| if ((chan < 1) || (chan > S2MU106_CH_MAX)) { |
| pr_info("%s: Wrong channel!!\n", __func__); |
| return -1; |
| } |
| |
| switch(chan) { |
| case 1: |
| dest = S2MU106_FLED_CH1_CTRL0; |
| break; |
| case 2: |
| dest = S2MU106_FLED_CH2_CTRL0; |
| break; |
| case 3: |
| dest = S2MU106_FLED_CH3_CTRL0; |
| break; |
| default: |
| return curr; |
| break; |
| } |
| |
| s2mu106_read_reg(fled->i2c, dest, &data); |
| |
| data = data & S2MU106_CHX_FLASH_IOUT; |
| curr = (data * 50) + 50; |
| |
| pr_info("%s: CH%02d flash curr. = %dmA\n", __func__, |
| chan, curr); |
| |
| return curr; |
| } |
| |
| static int s2mu106_fled_set_flash_curr(struct s2mu106_fled_data *fled, |
| int chan, int curr) |
| { |
| int ret = -1; |
| u8 data; |
| u8 dest; |
| int curr_set; |
| |
| if ((chan < 1) || (chan > S2MU106_CH_MAX)) { |
| pr_info("%s: Wrong channel!!\n", __func__); |
| return -1; |
| } |
| |
| switch(chan) { |
| case 1: |
| dest = S2MU106_FLED_CH1_CTRL0; |
| break; |
| case 2: |
| dest = S2MU106_FLED_CH2_CTRL0; |
| break; |
| case 3: |
| dest = S2MU106_FLED_CH3_CTRL0; |
| break; |
| default: |
| return ret; |
| break; |
| } |
| |
| if (curr < 50) |
| curr = 50; |
| else if (curr > s2mu106_fled_flash_curr_max[chan]) |
| curr = s2mu106_fled_flash_curr_max[chan]; |
| |
| data = (curr - 50)/50; |
| |
| s2mu106_update_reg(fled->i2c, dest, data, S2MU106_CHX_FLASH_IOUT); |
| |
| curr_set = s2mu106_fled_get_flash_curr(fled, chan); |
| |
| pr_info("%s: curr: %d, curr_set: %d\n", __func__, |
| curr, curr_set); |
| |
| return ret; |
| } |
| |
| static int s2mu106_fled_get_torch_curr(struct s2mu106_fled_data *fled, |
| int chan) |
| { |
| int curr = -1; |
| u8 data; |
| u8 dest; |
| |
| if ((chan < 1) || (chan > S2MU106_CH_MAX)) { |
| pr_info("%s: Wrong channel!!\n", __func__); |
| return -1; |
| } |
| |
| switch(chan) { |
| case 1: |
| dest = S2MU106_FLED_CH1_CTRL1; |
| break; |
| case 2: |
| dest = S2MU106_FLED_CH2_CTRL1; |
| break; |
| case 3: |
| dest = S2MU106_FLED_CH3_CTRL1; |
| break; |
| default: |
| return curr; |
| break; |
| } |
| |
| s2mu106_read_reg(fled->i2c, dest, &data); |
| |
| data = data & S2MU106_CHX_TORCH_IOUT; |
| curr = data * 10 + 10; |
| |
| pr_info("%s: CH%02d torch curr. = %dmA\n", __func__, |
| chan, curr); |
| |
| return curr; |
| } |
| |
| static int s2mu106_fled_set_torch_curr(struct s2mu106_fled_data *fled, |
| int chan, int curr) |
| { |
| int ret = -1; |
| u8 data; |
| u8 dest; |
| int curr_set; |
| |
| if ((chan < 1) || (chan > S2MU106_CH_MAX)) { |
| pr_info("%s: Wrong channel!!\n", __func__); |
| return -1; |
| } |
| |
| switch(chan) { |
| case 1: |
| dest = S2MU106_FLED_CH1_CTRL1; |
| break; |
| case 2: |
| dest = S2MU106_FLED_CH2_CTRL1; |
| break; |
| case 3: |
| dest = S2MU106_FLED_CH3_CTRL1; |
| break; |
| default: |
| return ret; |
| break; |
| } |
| |
| if (curr < 10) |
| curr = 10; |
| else if (curr > s2mu106_fled_torch_curr_max[chan]) |
| curr = s2mu106_fled_torch_curr_max[chan]; |
| |
| data = (curr - 10)/10; |
| |
| s2mu106_update_reg(fled->i2c, dest, data, S2MU106_CHX_TORCH_IOUT); |
| |
| curr_set = s2mu106_fled_get_torch_curr(fled, chan); |
| |
| pr_info("%s: curr: %d, curr_set: %d\n", __func__, |
| curr, curr_set); |
| |
| ret = 0; |
| |
| return ret; |
| |
| } |
| |
| static void s2mu106_fled_operating_mode(struct s2mu106_fled_data *fled, int mode) |
| { |
| u8 value; |
| |
| if (fled->set_on_factory) { |
| pr_info("%s Factory Status, Return\n", __func__); |
| return; |
| } |
| |
| if (mode < 0 || mode > 3) { |
| pr_info ("%s, wrong mode\n", __func__); |
| mode = AUTO_MODE; |
| } |
| |
| pr_info ("%s = %s\n", __func__, s2mu106_fled_operating_mode_string[mode]); |
| |
| value = mode << 6; |
| s2mu106_update_reg(fled->i2c, S2MU106_FLED_CTRL0, value, 0xC0); |
| } |
| |
| static int s2mu106_fled_get_mode(struct s2mu106_fled_data *fled, int chan) |
| { |
| u8 status; |
| int ret = -1; |
| |
| s2mu106_read_reg(fled->i2c, S2MU106_FLED_STATUS1, &status); |
| |
| pr_info("%s: S2MU106_FLED_STATUS1: 0x%02x\n", __func__, status); |
| |
| if ((chan < 1) || (chan > S2MU106_CH_MAX)) { |
| pr_info("%s: Wrong channel!!\n", __func__); |
| return -1; |
| } |
| |
| switch(chan) { |
| case 1: |
| if (status & S2MU106_CH1_FLASH_ON) |
| ret = S2MU106_FLED_MODE_FLASH; |
| else if (status & S2MU106_CH1_TORCH_ON) |
| ret = S2MU106_FLED_MODE_TORCH; |
| else |
| ret = S2MU106_FLED_MODE_OFF; |
| break; |
| case 2: |
| if (status & S2MU106_CH2_FLASH_ON) |
| ret = S2MU106_FLED_MODE_FLASH; |
| else if (status & S2MU106_CH2_TORCH_ON) |
| ret = S2MU106_FLED_MODE_TORCH; |
| else |
| ret = S2MU106_FLED_MODE_OFF; |
| break; |
| case 3: |
| if (status & S2MU106_CH3_FLASH_ON) |
| ret = S2MU106_FLED_MODE_FLASH; |
| else if (status & S2MU106_CH3_TORCH_ON) |
| ret = S2MU106_FLED_MODE_TORCH; |
| else |
| ret = S2MU106_FLED_MODE_OFF; |
| break; |
| default: |
| break; |
| } |
| return ret; |
| } |
| |
| static int s2mu106_fled_set_mode(struct s2mu106_fled_data *fled, |
| int chan, int mode) |
| { |
| u8 dest = 0, bit = 0, mask = 0, status = 0; |
| |
| if ((chan <= 0) || (chan > S2MU106_CH_MAX) || |
| (mode < 0) || (mode > S2MU106_FLED_MODE_MAX)) { |
| pr_err("%s: Wrong channel or mode.\n", __func__); |
| return -EFAULT; |
| } |
| |
| pr_err("%s: channel: %d, mode: %d\n", __func__, chan, mode); |
| |
| /* 0b000: OFF, 0b101: i2c bit control(on) */ |
| switch(mode) { |
| case S2MU106_FLED_MODE_OFF: |
| mask = S2MU106_CHX_FLASH_FLED_EN | |
| S2MU106_CHX_TORCH_FLED_EN; |
| bit = 0; |
| break; |
| case S2MU106_FLED_MODE_FLASH: |
| mask = S2MU106_CHX_FLASH_FLED_EN; |
| if (fled->control_mode == CONTROL_I2C) |
| bit = S2MU106_FLED_EN << 3; |
| else |
| bit = S2MU106_FLED_GPIO_EN1 << 3; |
| break; |
| case S2MU106_FLED_MODE_TORCH: |
| s2mu106_fled_operating_mode(fled, SYS_MODE); |
| mask = S2MU106_CHX_TORCH_FLED_EN; |
| if (fled->control_mode == CONTROL_I2C) |
| bit = S2MU106_FLED_EN; |
| else |
| bit = S2MU106_FLED_GPIO_EN2; |
| break; |
| default: |
| return -EFAULT; |
| break; |
| } |
| |
| switch(chan) { |
| case 1: |
| dest = S2MU106_FLED_CTRL1; |
| break; |
| case 2: |
| dest = S2MU106_FLED_CTRL2; |
| break; |
| case 3: |
| dest = S2MU106_FLED_CTRL3; |
| break; |
| default: |
| return -EFAULT; |
| break; |
| } |
| |
| /* Need to set EN_FLED_PRE bit before mode change */ |
| if (mode != S2MU106_FLED_MODE_OFF) |
| s2mu106_update_reg(fled->i2c, S2MU106_FLED_CTRL0, |
| S2MU106_EN_FLED_PRE, S2MU106_EN_FLED_PRE); |
| else { |
| /* If no LED is on, clear EN_FLED_PRE */ |
| s2mu106_read_reg(fled->i2c, S2MU106_FLED_STATUS1, &status); |
| if (!(status & S2MU106_FLED_ON_CHECK)) |
| s2mu106_update_reg(fled->i2c, S2MU106_FLED_CTRL0, |
| 0, S2MU106_EN_FLED_PRE); |
| } |
| |
| s2mu106_update_reg(fled->i2c, dest, bit, mask); |
| |
| if (mode == S2MU106_FLED_MODE_OFF) |
| s2mu106_fled_operating_mode(fled, AUTO_MODE); |
| |
| return 0; |
| } |
| |
| int s2mu106_led_mode_ctrl(int state) |
| { |
| struct s2mu106_fled_data *fled = g_fled_data; |
| union power_supply_propval value; |
| int gpio_torch = fled->torch_gpio; |
| int gpio_flash = fled->flash_gpio; |
| |
| pr_info("%s : state = %d\n", __func__, state); |
| |
| gpio_request(gpio_torch, "s2mu106_gpio_torch"); |
| gpio_request(gpio_flash, "s2mu106_gpio_flash"); |
| |
| switch(state) { |
| case S2MU106_FLED_MODE_OFF: |
| gpio_direction_output(gpio_torch, 0); |
| gpio_direction_output(gpio_flash, 0); |
| if(s2mu106_fled_get_torch_curr(fled, 1) != fled->preflash_current/10) |
| s2mu106_fled_set_torch_curr(fled, 1, fled->preflash_current); |
| |
| if (fled->is_en_flash) { |
| if (!fled->psy_chg) |
| fled->psy_chg = power_supply_get_by_name("s2mu106-charger"); |
| fled->is_en_flash = (value.intval = false); |
| power_supply_set_property(fled->psy_chg, |
| POWER_SUPPLY_PROP_ENERGY_AVG, &value); |
| } |
| s2mu106_fled_operating_mode(fled, SYS_MODE); |
| break; |
| case S2MU106_FLED_MODE_TORCH: |
| /* chgange SYS MODE when turn on torch */ |
| if((s2mu106_fled_get_torch_curr(fled, 1)/10) != fled->preflash_current/10) |
| s2mu106_fled_set_torch_curr(fled, 1, fled->preflash_current); |
| s2mu106_fled_operating_mode(fled, SYS_MODE); |
| gpio_direction_output(gpio_torch, 1); |
| break; |
| case S2MU106_FLED_MODE_FLASH: |
| if (!fled->psy_chg) |
| fled->psy_chg = power_supply_get_by_name("s2mu106-charger"); |
| |
| fled->is_en_flash = (value.intval = true); |
| power_supply_set_property(fled->psy_chg, |
| POWER_SUPPLY_PROP_ENERGY_AVG, &value); |
| |
| s2mu106_fled_operating_mode(fled, AUTO_MODE); |
| |
| gpio_direction_output(gpio_flash, 1); |
| break; |
| case S2MU106_FLED_MODE_MOVIE: |
| s2mu106_fled_operating_mode(fled, SYS_MODE); |
| s2mu106_fled_set_torch_curr(fled, 1, fled->movie_current); |
| gpio_direction_output(gpio_torch, 1); |
| break; |
| case S2MU106_FLED_MODE_FACTORY: |
| /* chgange SYS MODE when turn on torch */ |
| s2mu106_fled_operating_mode(fled, SYS_MODE); |
| gpio_direction_output(gpio_torch, 1); |
| break; |
| default: |
| break; |
| } |
| |
| gpio_free(gpio_torch); |
| gpio_free(gpio_flash); |
| |
| return 0; |
| } |
| |
| int s2mu106_mode_change_cam_to_leds(enum cam_flash_mode cam_mode) |
| { |
| int mode = -1; |
| |
| switch(cam_mode) { |
| case CAM_FLASH_MODE_OFF: |
| mode = S2MU106_FLED_MODE_OFF; |
| break; |
| case CAM_FLASH_MODE_SINGLE: |
| mode = S2MU106_FLED_MODE_FLASH; |
| break; |
| case CAM_FLASH_MODE_TORCH: |
| mode = S2MU106_FLED_MODE_TORCH; |
| break; |
| default: |
| mode = S2MU106_FLED_MODE_OFF; |
| break; |
| } |
| |
| return mode; |
| } |
| |
| int s2mu106_fled_set_mode_ctrl(int chan, enum cam_flash_mode cam_mode) |
| { |
| struct s2mu106_fled_data *fled = g_fled_data; |
| int mode = -1; |
| |
| mode = s2mu106_mode_change_cam_to_leds(cam_mode); |
| |
| if ((chan <= 0) || (chan > S2MU106_CH_MAX) || |
| (mode < 0) || (mode >= S2MU106_FLED_MODE_MAX)) { |
| pr_err("%s: channel: %d, mode: %d\n", __func__, chan, mode); |
| pr_err("%s: Wrong channel or mode.\n", __func__); |
| return -1; |
| } |
| |
| s2mu106_fled_set_mode(fled, chan, mode); |
| s2mu106_fled_test_read(fled); |
| |
| return 0; |
| } |
| |
| int s2mu106_fled_set_curr(int chan, enum cam_flash_mode cam_mode, int curr) |
| { |
| struct s2mu106_fled_data *fled = g_fled_data; |
| int mode = -1; |
| |
| mode = s2mu106_mode_change_cam_to_leds(cam_mode); |
| |
| /* Check channel */ |
| if ((chan <= 0) || (chan > S2MU106_CH_MAX)) { |
| pr_err("%s: Wrong channel.\n", __func__); |
| return -EFAULT; |
| } |
| |
| switch (mode){ |
| case S2MU106_FLED_MODE_TORCH: |
| /* Set curr. */ |
| s2mu106_fled_set_torch_curr(fled, chan, curr); |
| break; |
| case S2MU106_FLED_MODE_FLASH: |
| /* Set curr. */ |
| s2mu106_fled_set_flash_curr(fled, chan, curr); |
| break; |
| default: |
| return -1; |
| } |
| /* Test read */ |
| s2mu106_fled_test_read(fled); |
| |
| return 0; |
| } |
| |
| int s2mu106_fled_get_curr(int chan, enum cam_flash_mode cam_mode) |
| { |
| struct s2mu106_fled_data *fled = g_fled_data; |
| int mode = -1; |
| int curr = 0; |
| |
| mode = s2mu106_mode_change_cam_to_leds(cam_mode); |
| |
| /* Check channel */ |
| if ((chan <= 0) || (chan > S2MU106_CH_MAX)) { |
| pr_err("%s: Wrong channel.\n", __func__); |
| return -EFAULT; |
| } |
| |
| switch (mode){ |
| case S2MU106_FLED_MODE_TORCH: |
| curr = s2mu106_fled_get_torch_curr(fled, chan); |
| break; |
| case S2MU106_FLED_MODE_FLASH: |
| curr = s2mu106_fled_get_flash_curr(fled, chan); |
| break; |
| default: |
| return -1; |
| } |
| /* Test read */ |
| s2mu106_fled_test_read(fled); |
| |
| return curr; |
| } |
| |
| static ssize_t fled_flash_curr_show(struct device *dev, |
| struct device_attribute *attr, char *buf) |
| { |
| struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| struct s2mu106_fled_data *fled = |
| container_of(led_cdev, struct s2mu106_fled_data, cdev); |
| int cnt = 0; |
| int curr = 0; |
| int i; |
| char str[1016] = {0,}; |
| |
| /* Read curr. */ |
| for (i = 1; i <= S2MU106_CH_MAX; i++) { |
| curr = s2mu106_fled_get_flash_curr(fled, i); |
| pr_info("%s: channel: %d, curr: %dmA\n", __func__, i, curr); |
| if (curr >= 0) |
| cnt += sprintf(str+strlen(str), "CH%02d: %dmA, ", i, curr); |
| } |
| |
| cnt += sprintf(str+strlen(str), "\n"); |
| |
| strcpy(buf, str); |
| |
| return cnt; |
| } |
| |
| static ssize_t fled_flash_curr_store(struct device *dev, |
| struct device_attribute *attr, const char *buf, size_t size) |
| { |
| struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| struct s2mu106_fled_data *fled = |
| container_of(led_cdev, struct s2mu106_fled_data, cdev); |
| int chan = -1; |
| int curr = -1; |
| |
| sscanf(buf, "%d %d", &chan, &curr); |
| |
| /* Check channel */ |
| if ((chan <= 0) || (chan > S2MU106_CH_MAX)) { |
| pr_err("%s: Wrong channel.\n", __func__); |
| return -EFAULT; |
| } |
| |
| /* Set curr. */ |
| s2mu106_fled_set_flash_curr(fled, chan, curr); |
| |
| /* Test read */ |
| s2mu106_fled_test_read(fled); |
| |
| return size; |
| } |
| |
| |
| static ssize_t fled_torch_curr_show(struct device *dev, |
| struct device_attribute *attr, char *buf) |
| { |
| struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| struct s2mu106_fled_data *fled = |
| container_of(led_cdev, struct s2mu106_fled_data, cdev); |
| int cnt = 0; |
| int curr = 0; |
| int i; |
| char str[1016] = {0,}; |
| |
| /* Read curr. */ |
| for (i = 1; i <= S2MU106_CH_MAX; i++) { |
| curr = s2mu106_fled_get_torch_curr(fled, i); |
| pr_info("%s: channel: %d, curr: %dmA\n", __func__, i, curr); |
| if (curr >= 0) |
| cnt += sprintf(str+strlen(str), "CH%02d: %dmA, ", i, curr); |
| } |
| |
| cnt += sprintf(str+strlen(str), "\n"); |
| |
| strcpy(buf, str); |
| |
| return cnt; |
| } |
| |
| static ssize_t fled_torch_curr_store(struct device *dev, |
| struct device_attribute *attr, const char *buf, size_t size) |
| { |
| struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| struct s2mu106_fled_data *fled = |
| container_of(led_cdev, struct s2mu106_fled_data, cdev); |
| int chan = -1; |
| int curr = -1; |
| |
| sscanf(buf, "%d %d", &chan, &curr); |
| |
| /* Check channel */ |
| if ((chan <= 0) || (chan > S2MU106_CH_MAX)) { |
| pr_err("%s: Wrong channel.\n", __func__); |
| return -EFAULT; |
| } |
| |
| /* Set curr. */ |
| s2mu106_fled_set_torch_curr(fled, chan, curr); |
| |
| /* Test read */ |
| s2mu106_fled_test_read(fled); |
| |
| return size; |
| } |
| |
| static ssize_t fled_mode_show(struct device *dev, |
| struct device_attribute *attr, char *buf) |
| { |
| struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| struct s2mu106_fled_data *fled = |
| container_of(led_cdev, struct s2mu106_fled_data, cdev); |
| int cnt = 0; |
| int mode = 0; |
| int i; |
| char str[1016] = {0,}; |
| |
| s2mu106_fled_test_read(fled); |
| |
| for (i = 1; i <= S2MU106_CH_MAX; i++) { |
| mode = s2mu106_fled_get_mode(fled, i); |
| if (mode >= 0) |
| cnt += sprintf(str+strlen(str), "CH%02d: %s, ", i, |
| s2mu106_fled_mode_string[mode]); |
| } |
| |
| cnt += sprintf(str+strlen(str), "\n"); |
| |
| strcpy(buf, str); |
| |
| return cnt; |
| } |
| |
| static ssize_t fled_mode_store(struct device *dev, |
| struct device_attribute *attr, const char *buf, size_t size) |
| { |
| struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| struct s2mu106_fled_data *fled = |
| container_of(led_cdev, struct s2mu106_fled_data, cdev); |
| int chan = -1; |
| int mode = -1; |
| |
| sscanf(buf, "%d %d", &chan, &mode); |
| |
| if ((chan <= 0) || (chan > S2MU106_CH_MAX) || |
| (mode < 0) || (mode >= S2MU106_FLED_MODE_MAX)) { |
| pr_err("%s: channel: %d, mode: %d\n", __func__, chan, mode); |
| pr_err("%s: Wrong channel or mode.\n", __func__); |
| return -EFAULT; |
| } |
| |
| if (fled->control_mode == CONTROL_I2C) |
| s2mu106_fled_set_mode(fled, chan, mode); |
| else { |
| if (mode == 1) |
| s2mu106_led_mode_ctrl(S2MU106_FLED_MODE_TORCH); |
| else if (mode == 2) |
| s2mu106_led_mode_ctrl(S2MU106_FLED_MODE_FLASH); |
| else |
| s2mu106_led_mode_ctrl(S2MU106_FLED_MODE_OFF); |
| } |
| |
| s2mu106_fled_test_read(fled); |
| |
| return size; |
| } |
| |
| static DEVICE_ATTR(fled_mode, 0644, fled_mode_show, fled_mode_store); |
| static DEVICE_ATTR(fled_flash_curr, 0644, fled_flash_curr_show, fled_flash_curr_store); |
| static DEVICE_ATTR(fled_torch_curr, 0644, fled_torch_curr_show, fled_torch_curr_store); |
| |
| static struct attribute *s2mu106_fled_attrs[] = { |
| &dev_attr_fled_mode.attr, |
| &dev_attr_fled_flash_curr.attr, |
| &dev_attr_fled_torch_curr.attr, |
| NULL |
| }; |
| ATTRIBUTE_GROUPS(s2mu106_fled); |
| |
| void s2mu106_fled_set_operation_mode(int mode) |
| { |
| if(mode) { |
| s2mu106_fled_operating_mode(g_fled_data, TA_MODE); |
| g_fled_data->set_on_factory = 1; |
| pr_info("%s: TA only mode set\n", __func__); |
| } |
| else { |
| g_fled_data->set_on_factory = 0; |
| s2mu106_fled_operating_mode(g_fled_data, AUTO_MODE); |
| pr_info("%s: Auto control mode set\n", __func__); |
| } |
| } |
| |
| static void s2mu106_fled_init(struct s2mu106_fled_data *fled) |
| { |
| int i; |
| pr_info("%s: s2mu106_fled init start\n", __func__); |
| |
| fled->is_en_flash = false; |
| if (gpio_is_valid(fled->pdata->flash_gpio) && |
| gpio_is_valid(fled->pdata->torch_gpio)) { |
| pr_info("%s: s2mu106_fled gpio mode\n", __func__); |
| fled->control_mode = CONTROL_GPIO; |
| gpio_request_one(fled->flash_gpio, GPIOF_OUT_INIT_LOW, "LED_GPIO_OUTPUT_LOW"); |
| gpio_request_one(fled->torch_gpio, GPIOF_OUT_INIT_LOW, "LED_GPIO_OUTPUT_LOW"); |
| gpio_free(fled->flash_gpio); |
| gpio_free(fled->torch_gpio); |
| /* CAM_FLASH_EN -> FLASH gpio mode */ |
| s2mu106_fled_set_mode(fled, 1, S2MU106_FLED_MODE_FLASH); |
| /* CAM_TORCH_EN -> TORCH gpio mode */ |
| s2mu106_fled_set_mode(fled, 1, S2MU106_FLED_MODE_TORCH); |
| |
| s2mu106_led_mode_ctrl(S2MU106_FLED_MODE_OFF); |
| } else { |
| fled->control_mode = CONTROL_I2C; |
| } |
| |
| /* FLED driver operating mode set, TA only mode*/ |
| fled->set_on_factory = 0; |
| if(factory_mode) { |
| s2mu106_fled_operating_mode(fled, TA_MODE); |
| fled->set_on_factory = 1; |
| } |
| |
| /* for Flash Auto boost */ |
| s2mu106_update_reg(fled->i2c, S2MU106_FLED_TEST3, 0x20, 0x20); |
| s2mu106_update_reg(fled->i2c, S2MU106_FLED_TEST4, 0x40, 0x40); |
| |
| for (i = 1; i <= S2MU106_CH_MAX; i++) { |
| s2mu106_fled_set_flash_curr(fled, i, fled->default_current); |
| s2mu106_fled_set_torch_curr(fled, i, fled->default_current); |
| } |
| /* flash current set */ |
| s2mu106_fled_set_flash_curr(fled, 1, fled->flash_current); |
| |
| /* torch current set */ |
| s2mu106_fled_set_torch_curr(fled, 1, fled->preflash_current); |
| |
| /* w/a: prevent SMPL event in case of flash operation */ |
| s2mu106_update_reg(fled->i2c, 0x21, 0x4, 0x7); |
| s2mu106_update_reg(fled->i2c, 0x89, 0x0, 0x3); |
| |
| fled->psy_chg = power_supply_get_by_name("s2mu106-charger"); |
| |
| s2mu106_fled_test_read(fled); |
| } |
| |
| #if defined(CONFIG_OF) |
| static int s2mu106_led_dt_parse_pdata(struct device *dev, |
| struct s2mu106_fled_platform_data *pdata) |
| { |
| struct device_node *led_np, *np, *c_np; |
| int ret; |
| u32 temp; |
| u32 index; |
| |
| led_np = dev->parent->of_node; |
| |
| if (!led_np) { |
| pr_err("<%s> could not find led sub-node led_np\n", __func__); |
| return -ENODEV; |
| } |
| |
| np = of_find_node_by_name(led_np, "flash_led"); |
| if (!np) { |
| pr_err("%s : could not find led sub-node np\n", __func__); |
| return -EINVAL; |
| } |
| |
| ret = of_property_read_u32(np, "default_current", |
| &pdata->default_current); |
| if (ret < 0) |
| pr_err("%s : could not find default_current\n", __func__); |
| |
| ret = of_property_read_u32(np, "max_current", |
| &pdata->max_current); |
| if (ret < 0) |
| pr_err("%s : could not find max_current\n", __func__); |
| |
| ret = of_property_read_u32(np, "default_timer", |
| &pdata->default_timer); |
| if (ret < 0) |
| pr_err("%s : could not find default_timer\n", __func__); |
| |
| ret = pdata->flash_gpio = of_get_named_gpio(np, "flash-gpio", 0); |
| if (ret < 0) { |
| pr_err("%s : can't get flash-gpio\n", __func__); |
| return ret; |
| } |
| |
| ret = pdata->torch_gpio = of_get_named_gpio(np, "torch-gpio", 0); |
| if (ret < 0) { |
| pr_err("%s : can't get torch-gpio\n", __func__); |
| return ret; |
| } |
| |
| ret = of_property_read_u32(np, "flash_current", |
| &pdata->flash_current); |
| if (ret < 0) |
| pr_err("%s : could not find flash_current\n", __func__); |
| |
| ret = of_property_read_u32(np, "preflash_current", |
| &pdata->preflash_current); |
| if (ret < 0) |
| pr_err("%s : could not find flash_current\n", __func__); |
| |
| ret = of_property_read_u32(np, "torch_current", |
| &pdata->torch_current); |
| if (ret < 0) |
| pr_err("%s : could not find torch_current\n", __func__); |
| |
| ret = of_property_read_u32(np, "movie_current", |
| &pdata->movie_current); |
| if (ret < 0) |
| pr_err("%s : could not find movie_current\n", __func__); |
| |
| ret = of_property_read_u32(np, "factory_current", |
| &pdata->factory_current); |
| if (ret < 0) |
| pr_err("%s : could not find factory_current\n", __func__); |
| |
| ret = of_property_read_u32_array(np, "flashlight_current", |
| pdata->flashlight_current, S2MU106_FLASH_LIGHT_MAX); |
| if (ret < 0) { |
| pr_err("%s : could not find flashlight_current\n", __func__); |
| |
| //default setting |
| pdata->flashlight_current[0] = 45; |
| pdata->flashlight_current[1] = 75; |
| pdata->flashlight_current[2] = 125; |
| pdata->flashlight_current[3] = 195; |
| pdata->flashlight_current[4] = 270; |
| } |
| |
| pdata->chan_num = of_get_child_count(np); |
| |
| if (pdata->chan_num > S2MU106_CH_MAX) |
| pdata->chan_num = S2MU106_CH_MAX; |
| |
| pdata->channel = devm_kzalloc(dev, |
| sizeof(struct s2mu106_fled_chan) * pdata->chan_num, GFP_KERNEL); |
| |
| for_each_child_of_node(np, c_np) { |
| ret = of_property_read_u32(c_np, "id", &temp); |
| if (ret < 0) |
| goto dt_err; |
| index = temp; |
| |
| pr_info("%s: temp = %d, index = %d\n", __func__, temp, index); |
| |
| if (index < S2MU106_CH_MAX) { |
| pdata->channel[index].id = index; |
| |
| ret = of_property_read_u32_index(np, "current", index, |
| &pdata->channel[index].curr); |
| if (ret < 0) { |
| pr_err("%s : could not find current for channel%d\n", |
| __func__, pdata->channel[index].id); |
| pdata->channel[index].curr = pdata->default_current; |
| } |
| |
| ret = of_property_read_u32_index(np, "timer", index, |
| &pdata->channel[index].timer); |
| if (ret < 0) { |
| pr_err("%s : could not find timer for channel%d\n", |
| __func__, pdata->channel[index].id); |
| pdata->channel[index].timer = pdata->default_timer; |
| } |
| } |
| } |
| return 0; |
| |
| dt_err: |
| pr_err("%s: DT parsing finish. ret = %d\n", __func__, ret); |
| return ret; |
| } |
| #endif /* CONFIG_OF */ |
| |
| static ssize_t rear_flash_store(struct device *dev, |
| struct device_attribute *attr, const char *buf, size_t size) |
| { |
| int mode = -1; |
| int value = 0; |
| int flash_current = 0; |
| int torch_current = 0; |
| |
| pr_info("%s: rear_flash_store start\n", __func__); |
| if ((buf == NULL) || kstrtouint(buf, 10, &value)) { |
| return -1; |
| } |
| |
| if ((value < 0)) { |
| pr_err("%s: value: %d\n", __func__, value); |
| pr_err("%s: Wrong mode.\n", __func__); |
| return -EFAULT; |
| } |
| pr_info("%s: %d: rear_flash_store:\n", __func__,value ); |
| g_fled_data->sysfs_input_data = value; |
| |
| flash_current = g_fled_data->flash_current; |
| torch_current = g_fled_data->torch_current; |
| |
| if (value <= 0) { |
| mode = S2MU106_FLED_MODE_OFF; |
| } else if (value == 1) { |
| mode = S2MU106_FLED_MODE_TORCH; |
| } else if (value == 100) { |
| /* Factory Torch*/ |
| pr_info("%s: factory torch current [%d]\n", __func__, g_fled_data->factory_current); |
| torch_current = g_fled_data->factory_current; |
| mode = S2MU106_FLED_MODE_TORCH; |
| } else if (value == 200) { |
| /* Factory Flash */ |
| pr_info("%s: factory flash current [%d]\n", __func__, g_fled_data->factory_current); |
| flash_current = g_fled_data->factory_current; |
| mode = S2MU106_FLED_MODE_FLASH; |
| } else if (value <= 1010 && value >= 1001) { |
| mode = S2MU106_FLED_MODE_TORCH; |
| /* (value) 1001, 1002, 1004, 1006, 1009 */ |
| if (value <= 1001) |
| torch_current = g_fled_data->flashlight_current[0]; |
| else if (value <= 1002) |
| torch_current = g_fled_data->flashlight_current[1]; |
| else if (value <= 1004) |
| torch_current = g_fled_data->flashlight_current[2]; |
| else if (value <= 1006) |
| torch_current = g_fled_data->flashlight_current[3]; |
| else if (value <= 1009) |
| torch_current = g_fled_data->flashlight_current[4]; |
| else |
| torch_current = g_fled_data->torch_current; |
| g_fled_data->sysfs_input_data = 1; |
| } else if (value == 2) { |
| mode = S2MU106_FLED_MODE_FLASH; |
| } |
| |
| mutex_lock(&g_fled_data->lock); |
| if (g_fled_data->control_mode == CONTROL_I2C) { |
| s2mu106_fled_set_mode(g_fled_data, 1, mode); |
| } else { |
| if (mode == S2MU106_FLED_MODE_TORCH) { |
| pr_info("%s: %d: S2MU106_FLED_MODE_FACTORY - %dmA\n", __func__, value, torch_current ); |
| /* torch current set */ |
| s2mu106_fled_set_torch_curr(g_fled_data, 1, torch_current); |
| s2mu106_led_mode_ctrl(S2MU106_FLED_MODE_FACTORY); |
| } else if (mode == S2MU106_FLED_MODE_FLASH) { |
| pr_info("%s: %d: S2MU106_FLED_MODE_FLASH - %dmA\n", __func__, value, flash_current ); |
| /* flash current set */ |
| s2mu106_fled_set_flash_curr(g_fled_data, 1, flash_current); |
| s2mu106_led_mode_ctrl(S2MU106_FLED_MODE_FLASH); |
| } else { |
| pr_info("%s: %d: S2MU106_FLED_MODE_OFF\n", __func__,value ); |
| |
| /* flase, torch current set for initial current */ |
| flash_current = g_fled_data->flash_current; |
| torch_current = g_fled_data->preflash_current; |
| |
| /* flash current set */ |
| s2mu106_fled_set_flash_curr(g_fled_data, 1, flash_current); |
| /* torch current set */ |
| s2mu106_fled_set_torch_curr(g_fled_data, 1, torch_current); |
| s2mu106_led_mode_ctrl(S2MU106_FLED_MODE_OFF); |
| } |
| } |
| |
| mutex_unlock(&g_fled_data->lock); |
| s2mu106_fled_test_read(g_fled_data); |
| pr_info("%s: rear_flash_store END\n", __func__); |
| return size; |
| } |
| |
| static ssize_t rear_flash_show(struct device *dev, |
| struct device_attribute *attr, char *buf) |
| { |
| return sprintf(buf, "%d\n", g_fled_data->sysfs_input_data); |
| } |
| |
| |
| static DEVICE_ATTR(rear_flash, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH, |
| rear_flash_show, rear_flash_store); |
| static DEVICE_ATTR(rear_torch_flash, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH, |
| rear_flash_show, rear_flash_store); |
| |
| int create_flash_sysfs(void) |
| { |
| int err = -ENODEV; |
| |
| if (IS_ERR_OR_NULL(camera_class)) { |
| pr_err("flash_sysfs: error, camera class not exist"); |
| return -ENODEV; |
| } |
| |
| flash_dev = device_create(camera_class, NULL, 0, NULL, "flash"); |
| if (IS_ERR(flash_dev)) { |
| pr_err("flash_sysfs: failed to create device(flash)\n"); |
| return -ENODEV; |
| } |
| err = device_create_file(flash_dev, &dev_attr_rear_flash); |
| if (unlikely(err < 0)) { |
| pr_err("flash_sysfs: failed to create device file, %s\n", |
| dev_attr_rear_flash.attr.name); |
| } |
| err = device_create_file(flash_dev, &dev_attr_rear_torch_flash); |
| if (unlikely(err < 0)) { |
| pr_err("flash_sysfs: failed to create device file, %s\n", |
| dev_attr_rear_torch_flash.attr.name); |
| } |
| |
| return 0; |
| } |
| |
| static int s2mu106_led_probe(struct platform_device *pdev) |
| { |
| int ret = 0; |
| int cnt = 0; |
| struct s2mu106_dev *s2mu106 = dev_get_drvdata(pdev->dev.parent); |
| struct s2mu106_fled_data *fled_data; |
| char name[20]; |
| |
| pr_info("%s: s2mu106_fled start\n", __func__); |
| |
| if (!s2mu106) { |
| dev_err(&pdev->dev, "drvdata->dev.parent not supplied\n"); |
| return -ENODEV; |
| } |
| |
| fled_data = devm_kzalloc(&pdev->dev, |
| sizeof(struct s2mu106_fled_data), GFP_KERNEL); |
| if (!fled_data) { |
| pr_err("%s: failed to allocate driver data\n", __func__); |
| return -ENOMEM; |
| } |
| |
| fled_data->dev = &pdev->dev; |
| fled_data->i2c = s2mu106->i2c; |
| fled_data->pdata = devm_kzalloc(&pdev->dev, |
| sizeof(*(fled_data->pdata)), GFP_KERNEL); |
| if (!fled_data->pdata) { |
| pr_err("%s: failed to allocate platform data\n", __func__); |
| return -ENOMEM; |
| } |
| |
| if (s2mu106->dev->of_node) { |
| ret = s2mu106_led_dt_parse_pdata(&pdev->dev, fled_data->pdata); |
| if (ret < 0) { |
| pr_err("%s: not found leds dt! ret=%d\n", |
| __func__, ret); |
| return -1; |
| } |
| } |
| |
| platform_set_drvdata(pdev, fled_data); |
| |
| /* Store fled_data for EXPORT_SYMBOL */ |
| g_fled_data = fled_data; |
| |
| snprintf(name, sizeof(name), "fled-s2mu106"); |
| fled_data->cdev.name = name; |
| fled_data->cdev.groups = s2mu106_fled_groups; |
| |
| ret = devm_led_classdev_register(&pdev->dev, &fled_data->cdev); |
| if (ret < 0) { |
| pr_err("%s: unable to register LED class dev\n", __func__); |
| return ret; |
| } |
| |
| g_fled_data->flash_gpio = fled_data->pdata->flash_gpio; |
| g_fled_data->torch_gpio = fled_data->pdata->torch_gpio; |
| g_fled_data->default_current = fled_data->pdata->default_current; |
| g_fled_data->flash_current = fled_data->pdata->flash_current; |
| g_fled_data->torch_current = fled_data->pdata->torch_current; |
| g_fled_data->preflash_current = fled_data->pdata->preflash_current; |
| g_fled_data->movie_current = fled_data->pdata->movie_current; |
| g_fled_data->factory_current = fled_data->pdata->factory_current; |
| |
| for (cnt = 0; cnt < S2MU106_FLASH_LIGHT_MAX; cnt++) { |
| g_fled_data->flashlight_current[cnt] = fled_data->pdata->flashlight_current[cnt]; |
| } |
| |
| s2mu106_fled_init(g_fled_data); |
| mutex_init(&fled_data->lock); |
| |
| //create sysfs for camera. |
| create_flash_sysfs(); |
| |
| pr_info("%s: s2mu106_fled loaded\n", __func__); |
| return 0; |
| } |
| |
| static int s2mu106_led_remove(struct platform_device *pdev) |
| { |
| device_remove_file(flash_dev, &dev_attr_rear_flash); |
| device_remove_file(flash_dev, &dev_attr_rear_torch_flash); |
| device_destroy(camera_class, 0); |
| class_destroy(camera_class); |
| mutex_destroy(&g_fled_data->lock); |
| return 0; |
| } |
| |
| static const struct platform_device_id s2mu106_leds_id[] = { |
| {"leds-s2mu106", 0}, |
| {}, |
| }; |
| |
| static struct platform_driver s2mu106_led_driver = { |
| .driver = { |
| .name = "leds-s2mu106", |
| .owner = THIS_MODULE, |
| }, |
| .probe = s2mu106_led_probe, |
| .remove = s2mu106_led_remove, |
| .id_table = s2mu106_leds_id, |
| }; |
| |
| static int __init s2mu106_led_driver_init(void) |
| { |
| return platform_driver_register(&s2mu106_led_driver); |
| } |
| module_init(s2mu106_led_driver_init); |
| |
| static void __exit s2mu106_led_driver_exit(void) |
| { |
| platform_driver_unregister(&s2mu106_led_driver); |
| } |
| module_exit(s2mu106_led_driver_exit); |
| |
| MODULE_AUTHOR("Keunho Hwang <keunho.hwang@samsung.com>"); |
| MODULE_DESCRIPTION("SAMSUNG s2mu106 flash LED Driver"); |
| MODULE_LICENSE("GPL"); |