Lines Matching +full:client +full:- +full:id

1 // SPDX-License-Identifier: GPL-2.0-only
3 * pca9532.c - 16-bit Led dimmer
18 #include <linux/leds-pca9532.h>
40 struct i2c_client *client; member
53 static int pca9532_probe(struct i2c_client *client,
54 const struct i2c_device_id *id);
55 static void pca9532_remove(struct i2c_client *client);
103 .name = "leds-pca953x",
116 static int pca9532_calcpwm(struct i2c_client *client, int pwm, int blink, in pca9532_calcpwm() argument
120 struct pca9532_data *data = i2c_get_clientdata(client); in pca9532_calcpwm()
121 for (i = 0; i < data->chip_info->num_leds; i++) { in pca9532_calcpwm()
122 if (data->leds[i].type == PCA9532_TYPE_LED && in pca9532_calcpwm()
123 data->leds[i].state == PCA9532_PWM0+pwm) { in pca9532_calcpwm()
125 b += data->leds[i].ldev.brightness; in pca9532_calcpwm()
129 dev_err(&client->dev, in pca9532_calcpwm()
132 return -EINVAL; in pca9532_calcpwm()
136 return -EINVAL; in pca9532_calcpwm()
137 data->pwm[pwm] = b; in pca9532_calcpwm()
138 data->psc[pwm] = blink; in pca9532_calcpwm()
142 static int pca9532_setpwm(struct i2c_client *client, int pwm) in pca9532_setpwm() argument
144 struct pca9532_data *data = i2c_get_clientdata(client); in pca9532_setpwm()
145 u8 maxleds = data->chip_info->num_leds; in pca9532_setpwm()
147 mutex_lock(&data->update_lock); in pca9532_setpwm()
148 i2c_smbus_write_byte_data(client, PCA9532_REG_PWM(maxleds, pwm), in pca9532_setpwm()
149 data->pwm[pwm]); in pca9532_setpwm()
150 i2c_smbus_write_byte_data(client, PCA9532_REG_PSC(maxleds, pwm), in pca9532_setpwm()
151 data->psc[pwm]); in pca9532_setpwm()
152 mutex_unlock(&data->update_lock); in pca9532_setpwm()
159 struct i2c_client *client = led->client; in pca9532_setled() local
160 struct pca9532_data *data = i2c_get_clientdata(client); in pca9532_setled()
161 u8 maxleds = data->chip_info->num_leds; in pca9532_setled()
164 mutex_lock(&data->update_lock); in pca9532_setled()
165 reg = i2c_smbus_read_byte_data(client, LED_REG(maxleds, led->id)); in pca9532_setled()
167 reg = reg & ~LED_MASK(led->id); in pca9532_setled()
169 reg = reg | (led->state << LED_SHIFT(led->id)); in pca9532_setled()
170 i2c_smbus_write_byte_data(client, LED_REG(maxleds, led->id), reg); in pca9532_setled()
171 mutex_unlock(&data->update_lock); in pca9532_setled()
181 led->state = PCA9532_OFF; in pca9532_set_brightness()
183 led->state = PCA9532_ON; in pca9532_set_brightness()
185 led->state = PCA9532_PWM0; /* Thecus: hardcode one pwm */ in pca9532_set_brightness()
186 err = pca9532_calcpwm(led->client, 0, 0, value); in pca9532_set_brightness()
190 if (led->state == PCA9532_PWM0) in pca9532_set_brightness()
191 pca9532_setpwm(led->client, 0); in pca9532_set_brightness()
200 struct i2c_client *client = led->client; in pca9532_set_blink() local
210 return -EINVAL; in pca9532_set_blink()
213 psc = (*delay_on * 152-1)/1000; in pca9532_set_blink()
214 err = pca9532_calcpwm(client, 0, psc, led_cdev->brightness); in pca9532_set_blink()
217 if (led->state == PCA9532_PWM0) in pca9532_set_blink()
218 pca9532_setpwm(led->client, 0); in pca9532_set_blink()
230 return -1; in pca9532_event()
234 data->pwm[1] = 127; in pca9532_event()
236 data->pwm[1] = 0; in pca9532_event()
238 schedule_work(&data->work); in pca9532_event()
247 u8 maxleds = data->chip_info->num_leds; in pca9532_input_work()
249 mutex_lock(&data->update_lock); in pca9532_input_work()
250 i2c_smbus_write_byte_data(data->client, PCA9532_REG_PWM(maxleds, 1), in pca9532_input_work()
251 data->pwm[1]); in pca9532_input_work()
252 mutex_unlock(&data->update_lock); in pca9532_input_work()
257 struct i2c_client *client = led->client; in pca9532_getled() local
258 struct pca9532_data *data = i2c_get_clientdata(client); in pca9532_getled()
259 u8 maxleds = data->chip_info->num_leds; in pca9532_getled()
263 mutex_lock(&data->update_lock); in pca9532_getled()
264 reg = i2c_smbus_read_byte_data(client, LED_REG(maxleds, led->id)); in pca9532_getled()
265 ret = (reg & LED_MASK(led->id)) >> LED_SHIFT(led->id); in pca9532_getled()
266 mutex_unlock(&data->update_lock); in pca9532_getled()
274 struct pca9532_led *led = &data->leds[offset]; in pca9532_gpio_request_pin()
276 if (led->type == PCA9532_TYPE_GPIO) in pca9532_gpio_request_pin()
279 return -EBUSY; in pca9532_gpio_request_pin()
285 struct pca9532_led *led = &data->leds[offset]; in pca9532_gpio_set_value()
288 led->state = PCA9532_ON; in pca9532_gpio_set_value()
290 led->state = PCA9532_OFF; in pca9532_gpio_set_value()
300 reg = i2c_smbus_read_byte_data(data->client, PCA9532_REG_INPUT(offset)); in pca9532_gpio_get_value()
325 while (--i >= 0) { in pca9532_destroy_devices()
326 switch (data->leds[i].type) { in pca9532_destroy_devices()
331 led_classdev_unregister(&data->leds[i].ldev); in pca9532_destroy_devices()
334 if (data->idev != NULL) { in pca9532_destroy_devices()
335 cancel_work_sync(&data->work); in pca9532_destroy_devices()
336 data->idev = NULL; in pca9532_destroy_devices()
343 if (data->gpio.parent) in pca9532_destroy_devices()
344 gpiochip_remove(&data->gpio); in pca9532_destroy_devices()
348 static int pca9532_configure(struct i2c_client *client, in pca9532_configure() argument
353 u8 maxleds = data->chip_info->num_leds; in pca9532_configure()
356 data->pwm[i] = pdata->pwm[i]; in pca9532_configure()
357 data->psc[i] = pdata->psc[i]; in pca9532_configure()
358 i2c_smbus_write_byte_data(client, PCA9532_REG_PWM(maxleds, i), in pca9532_configure()
359 data->pwm[i]); in pca9532_configure()
360 i2c_smbus_write_byte_data(client, PCA9532_REG_PSC(maxleds, i), in pca9532_configure()
361 data->psc[i]); in pca9532_configure()
364 for (i = 0; i < data->chip_info->num_leds; i++) { in pca9532_configure()
365 struct pca9532_led *led = &data->leds[i]; in pca9532_configure()
366 struct pca9532_led *pled = &pdata->leds[i]; in pca9532_configure()
367 led->client = client; in pca9532_configure()
368 led->id = i; in pca9532_configure()
369 led->type = pled->type; in pca9532_configure()
370 switch (led->type) { in pca9532_configure()
377 if (pled->state == PCA9532_KEEP) in pca9532_configure()
378 led->state = pca9532_getled(led); in pca9532_configure()
380 led->state = pled->state; in pca9532_configure()
381 led->name = pled->name; in pca9532_configure()
382 led->ldev.name = led->name; in pca9532_configure()
383 led->ldev.default_trigger = pled->default_trigger; in pca9532_configure()
384 led->ldev.brightness = LED_OFF; in pca9532_configure()
385 led->ldev.brightness_set_blocking = in pca9532_configure()
387 led->ldev.blink_set = pca9532_set_blink; in pca9532_configure()
388 err = led_classdev_register(&client->dev, &led->ldev); in pca9532_configure()
390 dev_err(&client->dev, in pca9532_configure()
392 led->name); in pca9532_configure()
398 BUG_ON(data->idev); in pca9532_configure()
399 led->state = PCA9532_PWM1; in pca9532_configure()
401 data->idev = devm_input_allocate_device(&client->dev); in pca9532_configure()
402 if (data->idev == NULL) { in pca9532_configure()
403 err = -ENOMEM; in pca9532_configure()
406 data->idev->name = pled->name; in pca9532_configure()
407 data->idev->phys = "i2c/pca9532"; in pca9532_configure()
408 data->idev->id.bustype = BUS_HOST; in pca9532_configure()
409 data->idev->id.vendor = 0x001f; in pca9532_configure()
410 data->idev->id.product = 0x0001; in pca9532_configure()
411 data->idev->id.version = 0x0100; in pca9532_configure()
412 data->idev->evbit[0] = BIT_MASK(EV_SND); in pca9532_configure()
413 data->idev->sndbit[0] = BIT_MASK(SND_BELL) | in pca9532_configure()
415 data->idev->event = pca9532_event; in pca9532_configure()
416 input_set_drvdata(data->idev, data); in pca9532_configure()
417 INIT_WORK(&data->work, pca9532_input_work); in pca9532_configure()
418 err = input_register_device(data->idev); in pca9532_configure()
420 cancel_work_sync(&data->work); in pca9532_configure()
421 data->idev = NULL; in pca9532_configure()
430 data->gpio.label = "gpio-pca9532"; in pca9532_configure()
431 data->gpio.direction_input = pca9532_gpio_direction_input; in pca9532_configure()
432 data->gpio.direction_output = pca9532_gpio_direction_output; in pca9532_configure()
433 data->gpio.set = pca9532_gpio_set_value; in pca9532_configure()
434 data->gpio.get = pca9532_gpio_get_value; in pca9532_configure()
435 data->gpio.request = pca9532_gpio_request_pin; in pca9532_configure()
436 data->gpio.can_sleep = 1; in pca9532_configure()
437 data->gpio.base = pdata->gpio_base; in pca9532_configure()
438 data->gpio.ngpio = data->chip_info->num_leds; in pca9532_configure()
439 data->gpio.parent = &client->dev; in pca9532_configure()
440 data->gpio.owner = THIS_MODULE; in pca9532_configure()
442 err = gpiochip_add_data(&data->gpio, data); in pca9532_configure()
444 /* Use data->gpio.dev as a flag for freeing gpiochip */ in pca9532_configure()
445 data->gpio.parent = NULL; in pca9532_configure()
446 dev_warn(&client->dev, "could not add gpiochip\n"); in pca9532_configure()
448 dev_info(&client->dev, "gpios %i...%i\n", in pca9532_configure()
449 data->gpio.base, data->gpio.base + in pca9532_configure()
450 data->gpio.ngpio - 1); in pca9532_configure()
476 return ERR_PTR(-ENOMEM); in pca9532_of_populate_pdata()
478 pdata->gpio_base = -1; in pca9532_of_populate_pdata()
480 of_property_read_u8_array(np, "nxp,pwm", &pdata->pwm[0], in pca9532_of_populate_pdata()
481 ARRAY_SIZE(pdata->pwm)); in pca9532_of_populate_pdata()
482 of_property_read_u8_array(np, "nxp,psc", &pdata->psc[0], in pca9532_of_populate_pdata()
483 ARRAY_SIZE(pdata->psc)); in pca9532_of_populate_pdata()
487 &pdata->leds[i].name)) in pca9532_of_populate_pdata()
488 pdata->leds[i].name = child->name; in pca9532_of_populate_pdata()
489 of_property_read_u32(child, "type", &pdata->leds[i].type); in pca9532_of_populate_pdata()
490 of_property_read_string(child, "linux,default-trigger", in pca9532_of_populate_pdata()
491 &pdata->leds[i].default_trigger); in pca9532_of_populate_pdata()
492 if (!of_property_read_string(child, "default-state", &state)) { in pca9532_of_populate_pdata()
494 pdata->leds[i].state = PCA9532_ON; in pca9532_of_populate_pdata()
496 pdata->leds[i].state = PCA9532_KEEP; in pca9532_of_populate_pdata()
507 static int pca9532_probe(struct i2c_client *client, in pca9532_probe() argument
508 const struct i2c_device_id *id) in pca9532_probe() argument
511 struct pca9532_data *data = i2c_get_clientdata(client); in pca9532_probe()
513 dev_get_platdata(&client->dev); in pca9532_probe()
514 struct device_node *np = dev_of_node(&client->dev); in pca9532_probe()
519 pca9532_of_populate_pdata(&client->dev, np); in pca9532_probe()
523 dev_err(&client->dev, "no platform data\n"); in pca9532_probe()
524 return -EINVAL; in pca9532_probe()
526 devid = (int)(uintptr_t)of_device_get_match_data(&client->dev); in pca9532_probe()
528 devid = id->driver_data; in pca9532_probe()
531 if (!i2c_check_functionality(client->adapter, in pca9532_probe()
533 return -EIO; in pca9532_probe()
535 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL); in pca9532_probe()
537 return -ENOMEM; in pca9532_probe()
539 data->chip_info = &pca9532_chip_info_tbl[devid]; in pca9532_probe()
541 dev_info(&client->dev, "setting platform data\n"); in pca9532_probe()
542 i2c_set_clientdata(client, data); in pca9532_probe()
543 data->client = client; in pca9532_probe()
544 mutex_init(&data->update_lock); in pca9532_probe()
546 return pca9532_configure(client, data, pca9532_pdata); in pca9532_probe()
549 static void pca9532_remove(struct i2c_client *client) in pca9532_remove() argument
551 struct pca9532_data *data = i2c_get_clientdata(client); in pca9532_remove()
553 pca9532_destroy_devices(data, data->chip_info->num_leds); in pca9532_remove()