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