1ee4e80b2SIsai Gaspar // SPDX-License-Identifier: GPL-2.0-only
2ee4e80b2SIsai Gaspar /*
3ee4e80b2SIsai Gaspar * LED driver for PCA995x I2C LED drivers
4ee4e80b2SIsai Gaspar *
5ee4e80b2SIsai Gaspar * Copyright 2011 bct electronic GmbH
6ee4e80b2SIsai Gaspar * Copyright 2013 Qtechnology/AS
7ee4e80b2SIsai Gaspar * Copyright 2022 NXP
8ee4e80b2SIsai Gaspar * Copyright 2023 Marek Vasut
9ee4e80b2SIsai Gaspar */
10ee4e80b2SIsai Gaspar
11ee4e80b2SIsai Gaspar #include <linux/i2c.h>
12ee4e80b2SIsai Gaspar #include <linux/leds.h>
13ee4e80b2SIsai Gaspar #include <linux/module.h>
14ee4e80b2SIsai Gaspar #include <linux/mod_devicetable.h>
15ee4e80b2SIsai Gaspar #include <linux/property.h>
16ee4e80b2SIsai Gaspar #include <linux/regmap.h>
17ee4e80b2SIsai Gaspar
18ee4e80b2SIsai Gaspar /* Register definition */
19ee4e80b2SIsai Gaspar #define PCA995X_MODE1 0x00
20ee4e80b2SIsai Gaspar #define PCA995X_MODE2 0x01
21ee4e80b2SIsai Gaspar #define PCA995X_LEDOUT0 0x02
22ee4e80b2SIsai Gaspar
23ee4e80b2SIsai Gaspar /* Auto-increment disabled. Normal mode */
24ee4e80b2SIsai Gaspar #define PCA995X_MODE1_CFG 0x00
25ee4e80b2SIsai Gaspar
26ee4e80b2SIsai Gaspar /* LED select registers determine the source that drives LED outputs */
27ee4e80b2SIsai Gaspar #define PCA995X_LED_OFF 0x0
28ee4e80b2SIsai Gaspar #define PCA995X_LED_ON 0x1
29ee4e80b2SIsai Gaspar #define PCA995X_LED_PWM_MODE 0x2
30ee4e80b2SIsai Gaspar #define PCA995X_LDRX_MASK 0x3
31ee4e80b2SIsai Gaspar #define PCA995X_LDRX_BITS 2
32ee4e80b2SIsai Gaspar
33dbba3fceSPieterjan Camerlynck #define PCA995X_MAX_OUTPUTS 24
34ee4e80b2SIsai Gaspar #define PCA995X_OUTPUTS_PER_REG 4
35ee4e80b2SIsai Gaspar
36ee4e80b2SIsai Gaspar #define PCA995X_IREFALL_FULL_CFG 0xFF
37ee4e80b2SIsai Gaspar #define PCA995X_IREFALL_HALF_CFG (PCA995X_IREFALL_FULL_CFG / 2)
38ee4e80b2SIsai Gaspar
39ee4e80b2SIsai Gaspar #define ldev_to_led(c) container_of(c, struct pca995x_led, ldev)
40ee4e80b2SIsai Gaspar
41dbba3fceSPieterjan Camerlynck struct pca995x_chipdef {
42dbba3fceSPieterjan Camerlynck unsigned int num_leds;
43dbba3fceSPieterjan Camerlynck u8 pwm_base;
44dbba3fceSPieterjan Camerlynck u8 irefall;
45dbba3fceSPieterjan Camerlynck };
46dbba3fceSPieterjan Camerlynck
47dbba3fceSPieterjan Camerlynck static const struct pca995x_chipdef pca9952_chipdef = {
48dbba3fceSPieterjan Camerlynck .num_leds = 16,
49dbba3fceSPieterjan Camerlynck .pwm_base = 0x0a,
50dbba3fceSPieterjan Camerlynck .irefall = 0x43,
51dbba3fceSPieterjan Camerlynck };
52dbba3fceSPieterjan Camerlynck
53dbba3fceSPieterjan Camerlynck static const struct pca995x_chipdef pca9955b_chipdef = {
54dbba3fceSPieterjan Camerlynck .num_leds = 16,
55dbba3fceSPieterjan Camerlynck .pwm_base = 0x08,
56dbba3fceSPieterjan Camerlynck .irefall = 0x45,
57dbba3fceSPieterjan Camerlynck };
58dbba3fceSPieterjan Camerlynck
59dbba3fceSPieterjan Camerlynck static const struct pca995x_chipdef pca9956b_chipdef = {
60dbba3fceSPieterjan Camerlynck .num_leds = 24,
61dbba3fceSPieterjan Camerlynck .pwm_base = 0x0a,
62dbba3fceSPieterjan Camerlynck .irefall = 0x40,
63dbba3fceSPieterjan Camerlynck };
64dbba3fceSPieterjan Camerlynck
65ee4e80b2SIsai Gaspar struct pca995x_led {
66ee4e80b2SIsai Gaspar unsigned int led_no;
67ee4e80b2SIsai Gaspar struct led_classdev ldev;
68ee4e80b2SIsai Gaspar struct pca995x_chip *chip;
69ee4e80b2SIsai Gaspar };
70ee4e80b2SIsai Gaspar
71ee4e80b2SIsai Gaspar struct pca995x_chip {
72ee4e80b2SIsai Gaspar struct regmap *regmap;
73ee4e80b2SIsai Gaspar struct pca995x_led leds[PCA995X_MAX_OUTPUTS];
74dbba3fceSPieterjan Camerlynck const struct pca995x_chipdef *chipdef;
75ee4e80b2SIsai Gaspar };
76ee4e80b2SIsai Gaspar
pca995x_brightness_set(struct led_classdev * led_cdev,enum led_brightness brightness)77ee4e80b2SIsai Gaspar static int pca995x_brightness_set(struct led_classdev *led_cdev,
78ee4e80b2SIsai Gaspar enum led_brightness brightness)
79ee4e80b2SIsai Gaspar {
80ee4e80b2SIsai Gaspar struct pca995x_led *led = ldev_to_led(led_cdev);
81ee4e80b2SIsai Gaspar struct pca995x_chip *chip = led->chip;
82dbba3fceSPieterjan Camerlynck const struct pca995x_chipdef *chipdef = chip->chipdef;
83ee4e80b2SIsai Gaspar u8 ledout_addr, pwmout_addr;
84ee4e80b2SIsai Gaspar int shift, ret;
85ee4e80b2SIsai Gaspar
86dbba3fceSPieterjan Camerlynck pwmout_addr = chipdef->pwm_base + led->led_no;
87ee4e80b2SIsai Gaspar ledout_addr = PCA995X_LEDOUT0 + (led->led_no / PCA995X_OUTPUTS_PER_REG);
88ee4e80b2SIsai Gaspar shift = PCA995X_LDRX_BITS * (led->led_no % PCA995X_OUTPUTS_PER_REG);
89ee4e80b2SIsai Gaspar
90ee4e80b2SIsai Gaspar switch (brightness) {
91ee4e80b2SIsai Gaspar case LED_FULL:
92ee4e80b2SIsai Gaspar return regmap_update_bits(chip->regmap, ledout_addr,
93ee4e80b2SIsai Gaspar PCA995X_LDRX_MASK << shift,
94ee4e80b2SIsai Gaspar PCA995X_LED_ON << shift);
95ee4e80b2SIsai Gaspar case LED_OFF:
96ee4e80b2SIsai Gaspar return regmap_update_bits(chip->regmap, ledout_addr,
97ee4e80b2SIsai Gaspar PCA995X_LDRX_MASK << shift, 0);
98ee4e80b2SIsai Gaspar default:
99ee4e80b2SIsai Gaspar /* Adjust brightness as per user input by changing individual PWM */
100ee4e80b2SIsai Gaspar ret = regmap_write(chip->regmap, pwmout_addr, brightness);
101ee4e80b2SIsai Gaspar if (ret)
102ee4e80b2SIsai Gaspar return ret;
103ee4e80b2SIsai Gaspar
104ee4e80b2SIsai Gaspar /*
105ee4e80b2SIsai Gaspar * Change LDRx configuration to individual brightness via PWM.
106ee4e80b2SIsai Gaspar * LED will stop blinking if it's doing so.
107ee4e80b2SIsai Gaspar */
108ee4e80b2SIsai Gaspar return regmap_update_bits(chip->regmap, ledout_addr,
109ee4e80b2SIsai Gaspar PCA995X_LDRX_MASK << shift,
110ee4e80b2SIsai Gaspar PCA995X_LED_PWM_MODE << shift);
111ee4e80b2SIsai Gaspar }
112ee4e80b2SIsai Gaspar }
113ee4e80b2SIsai Gaspar
114ee4e80b2SIsai Gaspar static const struct regmap_config pca995x_regmap = {
115ee4e80b2SIsai Gaspar .reg_bits = 8,
116ee4e80b2SIsai Gaspar .val_bits = 8,
117ee4e80b2SIsai Gaspar .max_register = 0x49,
118ee4e80b2SIsai Gaspar };
119ee4e80b2SIsai Gaspar
pca995x_probe(struct i2c_client * client)120ee4e80b2SIsai Gaspar static int pca995x_probe(struct i2c_client *client)
121ee4e80b2SIsai Gaspar {
122ee4e80b2SIsai Gaspar struct fwnode_handle *led_fwnodes[PCA995X_MAX_OUTPUTS] = { 0 };
123ee4e80b2SIsai Gaspar struct device *dev = &client->dev;
124dbba3fceSPieterjan Camerlynck const struct pca995x_chipdef *chipdef;
125ee4e80b2SIsai Gaspar struct pca995x_chip *chip;
126ee4e80b2SIsai Gaspar struct pca995x_led *led;
127*597c72f4SJavier Carrasco int i, j, reg, ret;
128ee4e80b2SIsai Gaspar
129dbba3fceSPieterjan Camerlynck chipdef = device_get_match_data(&client->dev);
130ee4e80b2SIsai Gaspar
131d14451d9SJavier Carrasco if (!dev_fwnode(dev))
132ee4e80b2SIsai Gaspar return -ENODEV;
133ee4e80b2SIsai Gaspar
134ee4e80b2SIsai Gaspar chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
135ee4e80b2SIsai Gaspar if (!chip)
136ee4e80b2SIsai Gaspar return -ENOMEM;
137ee4e80b2SIsai Gaspar
138dbba3fceSPieterjan Camerlynck chip->chipdef = chipdef;
139ee4e80b2SIsai Gaspar chip->regmap = devm_regmap_init_i2c(client, &pca995x_regmap);
140ee4e80b2SIsai Gaspar if (IS_ERR(chip->regmap))
141ee4e80b2SIsai Gaspar return PTR_ERR(chip->regmap);
142ee4e80b2SIsai Gaspar
143ee4e80b2SIsai Gaspar i2c_set_clientdata(client, chip);
144ee4e80b2SIsai Gaspar
145*597c72f4SJavier Carrasco device_for_each_child_node_scoped(dev, child) {
146ee4e80b2SIsai Gaspar ret = fwnode_property_read_u32(child, "reg", ®);
147d14451d9SJavier Carrasco if (ret)
148ee4e80b2SIsai Gaspar return ret;
149ee4e80b2SIsai Gaspar
150d14451d9SJavier Carrasco if (reg < 0 || reg >= PCA995X_MAX_OUTPUTS || led_fwnodes[reg])
151ee4e80b2SIsai Gaspar return -EINVAL;
152ee4e80b2SIsai Gaspar
153ee4e80b2SIsai Gaspar led = &chip->leds[reg];
154*597c72f4SJavier Carrasco led_fwnodes[reg] = fwnode_handle_get(child);
155ee4e80b2SIsai Gaspar led->chip = chip;
156ee4e80b2SIsai Gaspar led->led_no = reg;
157ee4e80b2SIsai Gaspar led->ldev.brightness_set_blocking = pca995x_brightness_set;
158ee4e80b2SIsai Gaspar led->ldev.max_brightness = 255;
159ee4e80b2SIsai Gaspar }
160ee4e80b2SIsai Gaspar
161ee4e80b2SIsai Gaspar for (i = 0; i < PCA995X_MAX_OUTPUTS; i++) {
162ee4e80b2SIsai Gaspar struct led_init_data init_data = {};
163ee4e80b2SIsai Gaspar
164ee4e80b2SIsai Gaspar if (!led_fwnodes[i])
165ee4e80b2SIsai Gaspar continue;
166ee4e80b2SIsai Gaspar
167ee4e80b2SIsai Gaspar init_data.fwnode = led_fwnodes[i];
168ee4e80b2SIsai Gaspar
169ee4e80b2SIsai Gaspar ret = devm_led_classdev_register_ext(dev,
170ee4e80b2SIsai Gaspar &chip->leds[i].ldev,
171ee4e80b2SIsai Gaspar &init_data);
172ee4e80b2SIsai Gaspar if (ret < 0) {
173*597c72f4SJavier Carrasco for (j = i; j < PCA995X_MAX_OUTPUTS; j++)
174*597c72f4SJavier Carrasco fwnode_handle_put(led_fwnodes[j]);
175ee4e80b2SIsai Gaspar return dev_err_probe(dev, ret,
176ee4e80b2SIsai Gaspar "Could not register LED %s\n",
177ee4e80b2SIsai Gaspar chip->leds[i].ldev.name);
178ee4e80b2SIsai Gaspar }
179ee4e80b2SIsai Gaspar }
180ee4e80b2SIsai Gaspar
181ee4e80b2SIsai Gaspar /* Disable LED all-call address and set normal mode */
182ee4e80b2SIsai Gaspar ret = regmap_write(chip->regmap, PCA995X_MODE1, PCA995X_MODE1_CFG);
183ee4e80b2SIsai Gaspar if (ret)
184ee4e80b2SIsai Gaspar return ret;
185ee4e80b2SIsai Gaspar
186ee4e80b2SIsai Gaspar /* IREF Output current value for all LEDn outputs */
187dbba3fceSPieterjan Camerlynck return regmap_write(chip->regmap, chipdef->irefall, PCA995X_IREFALL_HALF_CFG);
188ee4e80b2SIsai Gaspar }
189ee4e80b2SIsai Gaspar
190ee4e80b2SIsai Gaspar static const struct i2c_device_id pca995x_id[] = {
191dbba3fceSPieterjan Camerlynck { "pca9952", .driver_data = (kernel_ulong_t)&pca9952_chipdef },
192dbba3fceSPieterjan Camerlynck { "pca9955b", .driver_data = (kernel_ulong_t)&pca9955b_chipdef },
193dbba3fceSPieterjan Camerlynck { "pca9956b", .driver_data = (kernel_ulong_t)&pca9956b_chipdef },
194ee4e80b2SIsai Gaspar {}
195ee4e80b2SIsai Gaspar };
196ee4e80b2SIsai Gaspar MODULE_DEVICE_TABLE(i2c, pca995x_id);
197ee4e80b2SIsai Gaspar
198ee4e80b2SIsai Gaspar static const struct of_device_id pca995x_of_match[] = {
199dbba3fceSPieterjan Camerlynck { .compatible = "nxp,pca9952", .data = &pca9952_chipdef },
200dbba3fceSPieterjan Camerlynck { .compatible = "nxp,pca9955b", . data = &pca9955b_chipdef },
201dbba3fceSPieterjan Camerlynck { .compatible = "nxp,pca9956b", .data = &pca9956b_chipdef },
202ee4e80b2SIsai Gaspar {},
203ee4e80b2SIsai Gaspar };
204179507fcSMarek Vasut MODULE_DEVICE_TABLE(of, pca995x_of_match);
205ee4e80b2SIsai Gaspar
206ee4e80b2SIsai Gaspar static struct i2c_driver pca995x_driver = {
207ee4e80b2SIsai Gaspar .driver = {
208ee4e80b2SIsai Gaspar .name = "leds-pca995x",
209ee4e80b2SIsai Gaspar .of_match_table = pca995x_of_match,
210ee4e80b2SIsai Gaspar },
211ee4e80b2SIsai Gaspar .probe = pca995x_probe,
212ee4e80b2SIsai Gaspar .id_table = pca995x_id,
213ee4e80b2SIsai Gaspar };
214ee4e80b2SIsai Gaspar module_i2c_driver(pca995x_driver);
215ee4e80b2SIsai Gaspar
216ee4e80b2SIsai Gaspar MODULE_AUTHOR("Isai Gaspar <isaiezequiel.gaspar@nxp.com>");
217ee4e80b2SIsai Gaspar MODULE_DESCRIPTION("PCA995x LED driver");
218ee4e80b2SIsai Gaspar MODULE_LICENSE("GPL");
219