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