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