^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) * pca9532.c - 16-bit Led dimmer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2011 Jan Weitzel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2008 Riku Voipio
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Datasheet: http://www.nxp.com/documents/data_sheet/PCA9532.pdf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/leds.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/leds-pca9532.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/gpio/driver.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) /* m = num_leds*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define PCA9532_REG_INPUT(i) ((i) >> 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define PCA9532_REG_OFFSET(m) ((m) >> 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define PCA9532_REG_PSC(m, i) (PCA9532_REG_OFFSET(m) + 0x1 + (i) * 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define PCA9532_REG_PWM(m, i) (PCA9532_REG_OFFSET(m) + 0x2 + (i) * 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define LED_REG(m, led) (PCA9532_REG_OFFSET(m) + 0x5 + (led >> 2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define LED_NUM(led) (led & 0x3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define LED_SHIFT(led) (LED_NUM(led) * 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define LED_MASK(led) (0x3 << LED_SHIFT(led))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define ldev_to_led(c) container_of(c, struct pca9532_led, ldev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct pca9532_chip_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) u8 num_leds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct pca9532_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct pca9532_led leds[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct mutex update_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct input_dev *idev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct work_struct work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #ifdef CONFIG_LEDS_PCA9532_GPIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct gpio_chip gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) const struct pca9532_chip_info *chip_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) u8 pwm[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) u8 psc[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static int pca9532_probe(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) const struct i2c_device_id *id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) static int pca9532_remove(struct i2c_client *client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) pca9530,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) pca9531,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) pca9532,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) pca9533,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static const struct i2c_device_id pca9532_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) { "pca9530", pca9530 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) { "pca9531", pca9531 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) { "pca9532", pca9532 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) { "pca9533", pca9533 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) MODULE_DEVICE_TABLE(i2c, pca9532_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) static const struct pca9532_chip_info pca9532_chip_info_tbl[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) [pca9530] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) .num_leds = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) [pca9531] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) .num_leds = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) [pca9532] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) .num_leds = 16,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) [pca9533] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) .num_leds = 4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) },
^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) #ifdef CONFIG_OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) static const struct of_device_id of_pca9532_leds_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) { .compatible = "nxp,pca9530", .data = (void *)pca9530 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) { .compatible = "nxp,pca9531", .data = (void *)pca9531 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) { .compatible = "nxp,pca9532", .data = (void *)pca9532 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) { .compatible = "nxp,pca9533", .data = (void *)pca9533 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) {},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) MODULE_DEVICE_TABLE(of, of_pca9532_leds_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static struct i2c_driver pca9532_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) .name = "leds-pca953x",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) .of_match_table = of_match_ptr(of_pca9532_leds_match),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) .probe = pca9532_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) .remove = pca9532_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) .id_table = pca9532_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) /* We have two pwm/blinkers, but 16 possible leds to drive. Additionally,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * the clever Thecus people are using one pwm to drive the beeper. So,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * as a compromise we average one pwm to the values requested by all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * leds that are not ON/OFF.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static int pca9532_calcpwm(struct i2c_client *client, int pwm, int blink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) enum led_brightness value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) int a = 0, b = 0, i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct pca9532_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) for (i = 0; i < data->chip_info->num_leds; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (data->leds[i].type == PCA9532_TYPE_LED &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) data->leds[i].state == PCA9532_PWM0+pwm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) a++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) b += data->leds[i].ldev.brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (a == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) dev_err(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) "fear of division by zero %d/%d, wanted %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) b, a, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) b = b/a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (b > 0xFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) data->pwm[pwm] = b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) data->psc[pwm] = blink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static int pca9532_setpwm(struct i2c_client *client, int pwm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) struct pca9532_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) u8 maxleds = data->chip_info->num_leds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) i2c_smbus_write_byte_data(client, PCA9532_REG_PWM(maxleds, pwm),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) data->pwm[pwm]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) i2c_smbus_write_byte_data(client, PCA9532_REG_PSC(maxleds, pwm),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) data->psc[pwm]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) /* Set LED routing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static void pca9532_setled(struct pca9532_led *led)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) struct i2c_client *client = led->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) struct pca9532_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) u8 maxleds = data->chip_info->num_leds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) char reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) reg = i2c_smbus_read_byte_data(client, LED_REG(maxleds, led->id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) /* zero led bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) reg = reg & ~LED_MASK(led->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) /* set the new value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) reg = reg | (led->state << LED_SHIFT(led->id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) i2c_smbus_write_byte_data(client, LED_REG(maxleds, led->id), reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) static int pca9532_set_brightness(struct led_classdev *led_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) enum led_brightness value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) struct pca9532_led *led = ldev_to_led(led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (value == LED_OFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) led->state = PCA9532_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) else if (value == LED_FULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) led->state = PCA9532_ON;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) led->state = PCA9532_PWM0; /* Thecus: hardcode one pwm */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) err = pca9532_calcpwm(led->client, 0, 0, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (led->state == PCA9532_PWM0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) pca9532_setpwm(led->client, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) pca9532_setled(led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) static int pca9532_set_blink(struct led_classdev *led_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) unsigned long *delay_on, unsigned long *delay_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) struct pca9532_led *led = ldev_to_led(led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) struct i2c_client *client = led->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) int psc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) if (*delay_on == 0 && *delay_off == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) /* led subsystem ask us for a blink rate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) *delay_on = 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) *delay_off = 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) if (*delay_on != *delay_off || *delay_on > 1690 || *delay_on < 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) /* Thecus specific: only use PSC/PWM 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) psc = (*delay_on * 152-1)/1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) err = pca9532_calcpwm(client, 0, psc, led_cdev->brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (led->state == PCA9532_PWM0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) pca9532_setpwm(led->client, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) pca9532_setled(led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) static int pca9532_event(struct input_dev *dev, unsigned int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) unsigned int code, int value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) struct pca9532_data *data = input_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (!(type == EV_SND && (code == SND_BELL || code == SND_TONE)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) /* XXX: allow different kind of beeps with psc/pwm modifications */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) if (value > 1 && value < 32767)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) data->pwm[1] = 127;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) data->pwm[1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) schedule_work(&data->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) static void pca9532_input_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) struct pca9532_data *data =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) container_of(work, struct pca9532_data, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) u8 maxleds = data->chip_info->num_leds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) i2c_smbus_write_byte_data(data->client, PCA9532_REG_PWM(maxleds, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) data->pwm[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) static enum pca9532_state pca9532_getled(struct pca9532_led *led)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) struct i2c_client *client = led->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) struct pca9532_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) u8 maxleds = data->chip_info->num_leds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) char reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) enum pca9532_state ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) mutex_lock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) reg = i2c_smbus_read_byte_data(client, LED_REG(maxleds, led->id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) ret = (reg & LED_MASK(led->id)) >> LED_SHIFT(led->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) mutex_unlock(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) #ifdef CONFIG_LEDS_PCA9532_GPIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) static int pca9532_gpio_request_pin(struct gpio_chip *gc, unsigned offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) struct pca9532_data *data = gpiochip_get_data(gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) struct pca9532_led *led = &data->leds[offset];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) if (led->type == PCA9532_TYPE_GPIO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) static void pca9532_gpio_set_value(struct gpio_chip *gc, unsigned offset, int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) struct pca9532_data *data = gpiochip_get_data(gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) struct pca9532_led *led = &data->leds[offset];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) if (val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) led->state = PCA9532_ON;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) led->state = PCA9532_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) pca9532_setled(led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) static int pca9532_gpio_get_value(struct gpio_chip *gc, unsigned offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) struct pca9532_data *data = gpiochip_get_data(gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) unsigned char reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) reg = i2c_smbus_read_byte_data(data->client, PCA9532_REG_INPUT(offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) return !!(reg & (1 << (offset % 8)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) static int pca9532_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) /* To use as input ensure pin is not driven */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) pca9532_gpio_set_value(gc, offset, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) static int pca9532_gpio_direction_output(struct gpio_chip *gc, unsigned offset, int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) pca9532_gpio_set_value(gc, offset, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) #endif /* CONFIG_LEDS_PCA9532_GPIO */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) static int pca9532_destroy_devices(struct pca9532_data *data, int n_devs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) int i = n_devs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) while (--i >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) switch (data->leds[i].type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) case PCA9532_TYPE_NONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) case PCA9532_TYPE_GPIO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) case PCA9532_TYPE_LED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) led_classdev_unregister(&data->leds[i].ldev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) case PCA9532_TYPE_N2100_BEEP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) if (data->idev != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) cancel_work_sync(&data->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) data->idev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) #ifdef CONFIG_LEDS_PCA9532_GPIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) if (data->gpio.parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) gpiochip_remove(&data->gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) static int pca9532_configure(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) struct pca9532_data *data, struct pca9532_platform_data *pdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) int i, err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) int gpios = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) u8 maxleds = data->chip_info->num_leds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) for (i = 0; i < 2; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) data->pwm[i] = pdata->pwm[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) data->psc[i] = pdata->psc[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) i2c_smbus_write_byte_data(client, PCA9532_REG_PWM(maxleds, i),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) data->pwm[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) i2c_smbus_write_byte_data(client, PCA9532_REG_PSC(maxleds, i),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) data->psc[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) for (i = 0; i < data->chip_info->num_leds; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) struct pca9532_led *led = &data->leds[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) struct pca9532_led *pled = &pdata->leds[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) led->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) led->id = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) led->type = pled->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) switch (led->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) case PCA9532_TYPE_NONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) case PCA9532_TYPE_GPIO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) gpios++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) case PCA9532_TYPE_LED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) if (pled->state == PCA9532_KEEP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) led->state = pca9532_getled(led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) led->state = pled->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) led->name = pled->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) led->ldev.name = led->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) led->ldev.default_trigger = pled->default_trigger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) led->ldev.brightness = LED_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) led->ldev.brightness_set_blocking =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) pca9532_set_brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) led->ldev.blink_set = pca9532_set_blink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) err = led_classdev_register(&client->dev, &led->ldev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) dev_err(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) "couldn't register LED %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) led->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) pca9532_setled(led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) case PCA9532_TYPE_N2100_BEEP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) BUG_ON(data->idev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) led->state = PCA9532_PWM1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) pca9532_setled(led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) data->idev = devm_input_allocate_device(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) if (data->idev == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) data->idev->name = pled->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) data->idev->phys = "i2c/pca9532";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) data->idev->id.bustype = BUS_HOST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) data->idev->id.vendor = 0x001f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) data->idev->id.product = 0x0001;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) data->idev->id.version = 0x0100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) data->idev->evbit[0] = BIT_MASK(EV_SND);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) data->idev->sndbit[0] = BIT_MASK(SND_BELL) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) BIT_MASK(SND_TONE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) data->idev->event = pca9532_event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) input_set_drvdata(data->idev, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) INIT_WORK(&data->work, pca9532_input_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) err = input_register_device(data->idev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) cancel_work_sync(&data->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) data->idev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) #ifdef CONFIG_LEDS_PCA9532_GPIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) if (gpios) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) data->gpio.label = "gpio-pca9532";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) data->gpio.direction_input = pca9532_gpio_direction_input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) data->gpio.direction_output = pca9532_gpio_direction_output;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) data->gpio.set = pca9532_gpio_set_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) data->gpio.get = pca9532_gpio_get_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) data->gpio.request = pca9532_gpio_request_pin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) data->gpio.can_sleep = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) data->gpio.base = pdata->gpio_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) data->gpio.ngpio = data->chip_info->num_leds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) data->gpio.parent = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) data->gpio.owner = THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) err = gpiochip_add_data(&data->gpio, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) /* Use data->gpio.dev as a flag for freeing gpiochip */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) data->gpio.parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) dev_warn(&client->dev, "could not add gpiochip\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) dev_info(&client->dev, "gpios %i...%i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) data->gpio.base, data->gpio.base +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) data->gpio.ngpio - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) pca9532_destroy_devices(data, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) static struct pca9532_platform_data *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) pca9532_of_populate_pdata(struct device *dev, struct device_node *np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) struct pca9532_platform_data *pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) struct device_node *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) int devid, maxleds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) const char *state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) devid = (int)(uintptr_t)of_device_get_match_data(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) maxleds = pca9532_chip_info_tbl[devid].num_leds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) if (!pdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) of_property_read_u8_array(np, "nxp,pwm", &pdata->pwm[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) ARRAY_SIZE(pdata->pwm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) of_property_read_u8_array(np, "nxp,psc", &pdata->psc[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) ARRAY_SIZE(pdata->psc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) for_each_available_child_of_node(np, child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) if (of_property_read_string(child, "label",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) &pdata->leds[i].name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) pdata->leds[i].name = child->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) of_property_read_u32(child, "type", &pdata->leds[i].type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) of_property_read_string(child, "linux,default-trigger",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) &pdata->leds[i].default_trigger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) if (!of_property_read_string(child, "default-state", &state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) if (!strcmp(state, "on"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) pdata->leds[i].state = PCA9532_ON;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) else if (!strcmp(state, "keep"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) pdata->leds[i].state = PCA9532_KEEP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) if (++i >= maxleds) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) of_node_put(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) return pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) static int pca9532_probe(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) int devid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) struct pca9532_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) struct pca9532_platform_data *pca9532_pdata =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) dev_get_platdata(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) struct device_node *np = dev_of_node(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) if (!pca9532_pdata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) if (np) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) pca9532_pdata =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) pca9532_of_populate_pdata(&client->dev, np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) if (IS_ERR(pca9532_pdata))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) return PTR_ERR(pca9532_pdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) dev_err(&client->dev, "no platform data\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) devid = (int)(uintptr_t)of_device_get_match_data(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) devid = id->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) if (!i2c_check_functionality(client->adapter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) I2C_FUNC_SMBUS_BYTE_DATA))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) data->chip_info = &pca9532_chip_info_tbl[devid];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) dev_info(&client->dev, "setting platform data\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) i2c_set_clientdata(client, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) data->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) mutex_init(&data->update_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) return pca9532_configure(client, data, pca9532_pdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) static int pca9532_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) struct pca9532_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) return pca9532_destroy_devices(data, data->chip_info->num_leds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) module_i2c_driver(pca9532_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) MODULE_AUTHOR("Riku Voipio");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) MODULE_DESCRIPTION("PCA 9532 LED dimmer");