^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * leds-lp3944.c - driver for National Semiconductor LP3944 Funlight Chip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2009 Antonio Ospite <ospite@studenti.unina.it>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * I2C driver for National Semiconductor LP3944 Funlight Chip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * http://www.national.com/pf/LP/LP3944.html
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * This helper chip can drive up to 8 leds, with two programmable DIM modes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * it could even be used as a gpio expander but this driver assumes it is used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * as a led controller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * The DIM modes are used to set _blink_ patterns for leds, the pattern is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * specified supplying two parameters:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * - period: from 0s to 1.6s
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * - duty cycle: percentage of the period the led is on, from 0 to 100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * LP3944 can be found on Motorola A910 smartphone, where it drives the rgb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * leds, the camera flash light and the displays backlights.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/leds.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/leds-lp3944.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) /* Read Only Registers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define LP3944_REG_INPUT1 0x00 /* LEDs 0-7 InputRegister (Read Only) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define LP3944_REG_REGISTER1 0x01 /* None (Read Only) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #define LP3944_REG_PSC0 0x02 /* Frequency Prescaler 0 (R/W) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #define LP3944_REG_PWM0 0x03 /* PWM Register 0 (R/W) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #define LP3944_REG_PSC1 0x04 /* Frequency Prescaler 1 (R/W) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #define LP3944_REG_PWM1 0x05 /* PWM Register 1 (R/W) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #define LP3944_REG_LS0 0x06 /* LEDs 0-3 Selector (R/W) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #define LP3944_REG_LS1 0x07 /* LEDs 4-7 Selector (R/W) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) /* These registers are not used to control leds in LP3944, they can store
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * arbitrary values which the chip will ignore.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #define LP3944_REG_REGISTER8 0x08
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #define LP3944_REG_REGISTER9 0x09
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #define LP3944_DIM0 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #define LP3944_DIM1 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) /* period in ms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #define LP3944_PERIOD_MIN 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #define LP3944_PERIOD_MAX 1600
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) /* duty cycle is a percentage */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #define LP3944_DUTY_CYCLE_MIN 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #define LP3944_DUTY_CYCLE_MAX 100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #define ldev_to_led(c) container_of(c, struct lp3944_led_data, ldev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) /* Saved data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct lp3944_led_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) u8 id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) enum lp3944_type type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct led_classdev ldev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct lp3944_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct mutex lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct lp3944_led_data leds[LP3944_LEDS_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) static int lp3944_reg_read(struct i2c_client *client, u8 reg, u8 *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) int tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) tmp = i2c_smbus_read_byte_data(client, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (tmp < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) *value = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static int lp3944_reg_write(struct i2c_client *client, u8 reg, u8 value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return i2c_smbus_write_byte_data(client, reg, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * Set the period for DIM status
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * @client: the i2c client
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * @dim: either LP3944_DIM0 or LP3944_DIM1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * @period: period of a blink, that is a on/off cycle, expressed in ms.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static int lp3944_dim_set_period(struct i2c_client *client, u8 dim, u16 period)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) u8 psc_reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) u8 psc_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (dim == LP3944_DIM0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) psc_reg = LP3944_REG_PSC0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) else if (dim == LP3944_DIM1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) psc_reg = LP3944_REG_PSC1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) /* Convert period to Prescaler value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (period > LP3944_PERIOD_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) psc_value = (period * 255) / LP3944_PERIOD_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) err = lp3944_reg_write(client, psc_reg, psc_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * Set the duty cycle for DIM status
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * @client: the i2c client
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * @dim: either LP3944_DIM0 or LP3944_DIM1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * @duty_cycle: percentage of a period during which a led is ON
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static int lp3944_dim_set_dutycycle(struct i2c_client *client, u8 dim,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) u8 duty_cycle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) u8 pwm_reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) u8 pwm_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if (dim == LP3944_DIM0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) pwm_reg = LP3944_REG_PWM0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) else if (dim == LP3944_DIM1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) pwm_reg = LP3944_REG_PWM1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) /* Convert duty cycle to PWM value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (duty_cycle > LP3944_DUTY_CYCLE_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) pwm_value = (duty_cycle * 255) / LP3944_DUTY_CYCLE_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) err = lp3944_reg_write(client, pwm_reg, pwm_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * Set the led status
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * @led: a lp3944_led_data structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * @status: one of LP3944_LED_STATUS_OFF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * LP3944_LED_STATUS_ON
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * LP3944_LED_STATUS_DIM0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * LP3944_LED_STATUS_DIM1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static int lp3944_led_set(struct lp3944_led_data *led, u8 status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) struct lp3944_data *data = i2c_get_clientdata(led->client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) u8 id = led->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) u8 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) u8 val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) dev_dbg(&led->client->dev, "%s: %s, status before normalization:%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) __func__, led->ldev.name, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) switch (id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) case LP3944_LED0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) case LP3944_LED1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) case LP3944_LED2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) case LP3944_LED3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) reg = LP3944_REG_LS0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) case LP3944_LED4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) case LP3944_LED5:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) case LP3944_LED6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) case LP3944_LED7:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) id -= LP3944_LED4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) reg = LP3944_REG_LS1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (status > LP3944_LED_STATUS_DIM1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) * Invert status only when it's < 2 (i.e. 0 or 1) which means it's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * controlling the on/off state directly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * When, instead, status is >= 2 don't invert it because it would mean
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) * to mess with the hardware blinking mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) if (led->type == LP3944_LED_TYPE_LED_INVERTED && status < 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) status = 1 - status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) mutex_lock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) lp3944_reg_read(led->client, reg, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) val &= ~(LP3944_LED_STATUS_MASK << (id << 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) val |= (status << (id << 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) dev_dbg(&led->client->dev, "%s: %s, reg:%d id:%d status:%d val:%#x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) __func__, led->ldev.name, reg, id, status, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) /* set led status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) err = lp3944_reg_write(led->client, reg, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) mutex_unlock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) static int lp3944_led_set_blink(struct led_classdev *led_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) unsigned long *delay_on,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) unsigned long *delay_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) struct lp3944_led_data *led = ldev_to_led(led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) u16 period;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) u8 duty_cycle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) /* units are in ms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) if (*delay_on + *delay_off > LP3944_PERIOD_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) if (*delay_on == 0 && *delay_off == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) /* Special case: the leds subsystem requires a default user
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) * friendly blink pattern for the LED. Let's blink the led
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) * slowly (1Hz).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) *delay_on = 500;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) *delay_off = 500;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) period = (*delay_on) + (*delay_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) /* duty_cycle is the percentage of period during which the led is ON */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) duty_cycle = 100 * (*delay_on) / period;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) /* invert duty cycle for inverted leds, this has the same effect of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * swapping delay_on and delay_off
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) if (led->type == LP3944_LED_TYPE_LED_INVERTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) duty_cycle = 100 - duty_cycle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) /* NOTE: using always the first DIM mode, this means that all leds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) * will have the same blinking pattern.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) * We could find a way later to have two leds blinking in hardware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) * with different patterns at the same time, falling back to software
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) * control for the other ones.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) err = lp3944_dim_set_period(led->client, LP3944_DIM0, period);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) err = lp3944_dim_set_dutycycle(led->client, LP3944_DIM0, duty_cycle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) dev_dbg(&led->client->dev, "%s: OK hardware accelerated blink!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) lp3944_led_set(led, LP3944_LED_STATUS_DIM0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) static int lp3944_led_set_brightness(struct led_classdev *led_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) enum led_brightness brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) struct lp3944_led_data *led = ldev_to_led(led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) dev_dbg(&led->client->dev, "%s: %s, %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) __func__, led_cdev->name, brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) return lp3944_led_set(led, !!brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) static int lp3944_configure(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) struct lp3944_data *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) struct lp3944_platform_data *pdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) int i, err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) for (i = 0; i < pdata->leds_size; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) struct lp3944_led *pled = &pdata->leds[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) struct lp3944_led_data *led = &data->leds[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) led->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) led->id = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) switch (pled->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) case LP3944_LED_TYPE_LED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) case LP3944_LED_TYPE_LED_INVERTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) led->type = pled->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) led->ldev.name = pled->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) led->ldev.max_brightness = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) led->ldev.brightness_set_blocking =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) lp3944_led_set_brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) led->ldev.blink_set = lp3944_led_set_blink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) led->ldev.flags = LED_CORE_SUSPENDRESUME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) err = led_classdev_register(&client->dev, &led->ldev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) dev_err(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) "couldn't register LED %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) led->ldev.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) /* to expose the default value to userspace */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) led->ldev.brightness =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) (enum led_brightness) pled->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) /* Set the default led status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) err = lp3944_led_set(led, pled->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) dev_err(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) "%s couldn't set STATUS %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) led->ldev.name, pled->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) case LP3944_LED_TYPE_NONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) if (i > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) for (i = i - 1; i >= 0; i--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) switch (pdata->leds[i].type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) case LP3944_LED_TYPE_LED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) case LP3944_LED_TYPE_LED_INVERTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) led_classdev_unregister(&data->leds[i].ldev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) case LP3944_LED_TYPE_NONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) static int lp3944_probe(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) struct lp3944_platform_data *lp3944_pdata =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) dev_get_platdata(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) struct lp3944_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) if (lp3944_pdata == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) dev_err(&client->dev, "no platform data\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) /* Let's see whether this adapter can support what we need. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) if (!i2c_check_functionality(client->adapter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) I2C_FUNC_SMBUS_BYTE_DATA)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) dev_err(&client->dev, "insufficient functionality!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) data = devm_kzalloc(&client->dev, sizeof(struct lp3944_data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) data->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) i2c_set_clientdata(client, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) mutex_init(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) err = lp3944_configure(client, data, lp3944_pdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) dev_info(&client->dev, "lp3944 enabled\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) static int lp3944_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) struct lp3944_platform_data *pdata = dev_get_platdata(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) struct lp3944_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) for (i = 0; i < pdata->leds_size; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) switch (data->leds[i].type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) case LP3944_LED_TYPE_LED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) case LP3944_LED_TYPE_LED_INVERTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) led_classdev_unregister(&data->leds[i].ldev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) case LP3944_LED_TYPE_NONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) /* lp3944 i2c driver struct */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) static const struct i2c_device_id lp3944_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) {"lp3944", 0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) MODULE_DEVICE_TABLE(i2c, lp3944_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) static struct i2c_driver lp3944_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) .name = "lp3944",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) .probe = lp3944_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) .remove = lp3944_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) .id_table = lp3944_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) module_i2c_driver(lp3944_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) MODULE_DESCRIPTION("LP3944 Fun Light Chip");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) MODULE_LICENSE("GPL");