Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 1 | /* |
| 2 | * pca9532.c - 16-bit Led dimmer |
| 3 | * |
Riku Voipio | b26e0ed | 2009-03-03 21:37:17 +0200 | [diff] [blame] | 4 | * Copyright (C) 2008 Riku Voipio |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * Datasheet: http://www.nxp.com/acrobat/datasheets/PCA9532_3.pdf |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/i2c.h> |
| 16 | #include <linux/leds.h> |
| 17 | #include <linux/input.h> |
| 18 | #include <linux/mutex.h> |
Riku Voipio | 934cd3f | 2008-12-03 08:21:36 +0000 | [diff] [blame] | 19 | #include <linux/workqueue.h> |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 20 | #include <linux/leds-pca9532.h> |
| 21 | |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 22 | #define PCA9532_REG_PSC(i) (0x2+(i)*2) |
| 23 | #define PCA9532_REG_PWM(i) (0x3+(i)*2) |
| 24 | #define PCA9532_REG_LS0 0x6 |
| 25 | #define LED_REG(led) ((led>>2)+PCA9532_REG_LS0) |
| 26 | #define LED_NUM(led) (led & 0x3) |
| 27 | |
| 28 | #define ldev_to_led(c) container_of(c, struct pca9532_led, ldev) |
| 29 | |
| 30 | struct pca9532_data { |
| 31 | struct i2c_client *client; |
| 32 | struct pca9532_led leds[16]; |
| 33 | struct mutex update_lock; |
Richard Purdie | 85c5204 | 2009-09-07 14:35:04 +0100 | [diff] [blame] | 34 | struct input_dev *idev; |
Antonio Ospite | 07172d2 | 2009-06-19 13:53:07 +0200 | [diff] [blame] | 35 | struct work_struct work; |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 36 | u8 pwm[2]; |
| 37 | u8 psc[2]; |
| 38 | }; |
| 39 | |
| 40 | static int pca9532_probe(struct i2c_client *client, |
| 41 | const struct i2c_device_id *id); |
| 42 | static int pca9532_remove(struct i2c_client *client); |
| 43 | |
| 44 | static const struct i2c_device_id pca9532_id[] = { |
| 45 | { "pca9532", 0 }, |
| 46 | { } |
| 47 | }; |
| 48 | |
| 49 | MODULE_DEVICE_TABLE(i2c, pca9532_id); |
| 50 | |
| 51 | static struct i2c_driver pca9532_driver = { |
| 52 | .driver = { |
Richard Purdie | 85c5204 | 2009-09-07 14:35:04 +0100 | [diff] [blame] | 53 | .name = "pca9532", |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 54 | }, |
Richard Purdie | 85c5204 | 2009-09-07 14:35:04 +0100 | [diff] [blame] | 55 | .probe = pca9532_probe, |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 56 | .remove = pca9532_remove, |
| 57 | .id_table = pca9532_id, |
| 58 | }; |
| 59 | |
| 60 | /* We have two pwm/blinkers, but 16 possible leds to drive. Additionaly, |
| 61 | * the clever Thecus people are using one pwm to drive the beeper. So, |
| 62 | * as a compromise we average one pwm to the values requested by all |
| 63 | * leds that are not ON/OFF. |
| 64 | * */ |
Riku Voipio | 934cd3f | 2008-12-03 08:21:36 +0000 | [diff] [blame] | 65 | static int pca9532_calcpwm(struct i2c_client *client, int pwm, int blink, |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 66 | enum led_brightness value) |
| 67 | { |
| 68 | int a = 0, b = 0, i = 0; |
| 69 | struct pca9532_data *data = i2c_get_clientdata(client); |
| 70 | for (i = 0; i < 16; i++) { |
| 71 | if (data->leds[i].type == PCA9532_TYPE_LED && |
| 72 | data->leds[i].state == PCA9532_PWM0+pwm) { |
| 73 | a++; |
| 74 | b += data->leds[i].ldev.brightness; |
| 75 | } |
| 76 | } |
| 77 | if (a == 0) { |
| 78 | dev_err(&client->dev, |
| 79 | "fear of division by zero %d/%d, wanted %d\n", |
| 80 | b, a, value); |
| 81 | return -EINVAL; |
| 82 | } |
| 83 | b = b/a; |
| 84 | if (b > 0xFF) |
| 85 | return -EINVAL; |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 86 | data->pwm[pwm] = b; |
Antonio Ospite | 07172d2 | 2009-06-19 13:53:07 +0200 | [diff] [blame] | 87 | data->psc[pwm] = blink; |
| 88 | return 0; |
Riku Voipio | 934cd3f | 2008-12-03 08:21:36 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static int pca9532_setpwm(struct i2c_client *client, int pwm) |
| 92 | { |
Antonio Ospite | 07172d2 | 2009-06-19 13:53:07 +0200 | [diff] [blame] | 93 | struct pca9532_data *data = i2c_get_clientdata(client); |
| 94 | mutex_lock(&data->update_lock); |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 95 | i2c_smbus_write_byte_data(client, PCA9532_REG_PWM(pwm), |
| 96 | data->pwm[pwm]); |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 97 | i2c_smbus_write_byte_data(client, PCA9532_REG_PSC(pwm), |
| 98 | data->psc[pwm]); |
| 99 | mutex_unlock(&data->update_lock); |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | /* Set LED routing */ |
| 104 | static void pca9532_setled(struct pca9532_led *led) |
| 105 | { |
| 106 | struct i2c_client *client = led->client; |
| 107 | struct pca9532_data *data = i2c_get_clientdata(client); |
| 108 | char reg; |
| 109 | |
| 110 | mutex_lock(&data->update_lock); |
| 111 | reg = i2c_smbus_read_byte_data(client, LED_REG(led->id)); |
| 112 | /* zero led bits */ |
| 113 | reg = reg & ~(0x3<<LED_NUM(led->id)*2); |
| 114 | /* set the new value */ |
| 115 | reg = reg | (led->state << LED_NUM(led->id)*2); |
| 116 | i2c_smbus_write_byte_data(client, LED_REG(led->id), reg); |
| 117 | mutex_unlock(&data->update_lock); |
| 118 | } |
| 119 | |
| 120 | static void pca9532_set_brightness(struct led_classdev *led_cdev, |
| 121 | enum led_brightness value) |
| 122 | { |
| 123 | int err = 0; |
| 124 | struct pca9532_led *led = ldev_to_led(led_cdev); |
| 125 | |
| 126 | if (value == LED_OFF) |
| 127 | led->state = PCA9532_OFF; |
| 128 | else if (value == LED_FULL) |
| 129 | led->state = PCA9532_ON; |
| 130 | else { |
| 131 | led->state = PCA9532_PWM0; /* Thecus: hardcode one pwm */ |
Antonio Ospite | 07172d2 | 2009-06-19 13:53:07 +0200 | [diff] [blame] | 132 | err = pca9532_calcpwm(led->client, 0, 0, value); |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 133 | if (err) |
| 134 | return; /* XXX: led api doesn't allow error code? */ |
| 135 | } |
Antonio Ospite | 07172d2 | 2009-06-19 13:53:07 +0200 | [diff] [blame] | 136 | schedule_work(&led->work); |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | static int pca9532_set_blink(struct led_classdev *led_cdev, |
| 140 | unsigned long *delay_on, unsigned long *delay_off) |
| 141 | { |
| 142 | struct pca9532_led *led = ldev_to_led(led_cdev); |
| 143 | struct i2c_client *client = led->client; |
| 144 | int psc; |
Antonio Ospite | 07172d2 | 2009-06-19 13:53:07 +0200 | [diff] [blame] | 145 | int err = 0; |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 146 | |
| 147 | if (*delay_on == 0 && *delay_off == 0) { |
| 148 | /* led subsystem ask us for a blink rate */ |
Richard Purdie | 85c5204 | 2009-09-07 14:35:04 +0100 | [diff] [blame] | 149 | *delay_on = 1000; |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 150 | *delay_off = 1000; |
| 151 | } |
| 152 | if (*delay_on != *delay_off || *delay_on > 1690 || *delay_on < 6) |
| 153 | return -EINVAL; |
| 154 | |
| 155 | /* Thecus specific: only use PSC/PWM 0 */ |
| 156 | psc = (*delay_on * 152-1)/1000; |
Antonio Ospite | 07172d2 | 2009-06-19 13:53:07 +0200 | [diff] [blame] | 157 | err = pca9532_calcpwm(client, 0, psc, led_cdev->brightness); |
| 158 | if (err) |
| 159 | return err; |
| 160 | schedule_work(&led->work); |
| 161 | return 0; |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 162 | } |
| 163 | |
Sven Wegener | 0d73357 | 2008-11-17 14:33:41 +0000 | [diff] [blame] | 164 | static int pca9532_event(struct input_dev *dev, unsigned int type, |
| 165 | unsigned int code, int value) |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 166 | { |
| 167 | struct pca9532_data *data = input_get_drvdata(dev); |
| 168 | |
Riku Voipio | 7fbc3a9 | 2009-03-03 22:13:06 +0200 | [diff] [blame] | 169 | if (!(type == EV_SND && (code == SND_BELL || code == SND_TONE))) |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 170 | return -1; |
| 171 | |
| 172 | /* XXX: allow different kind of beeps with psc/pwm modifications */ |
| 173 | if (value > 1 && value < 32767) |
| 174 | data->pwm[1] = 127; |
| 175 | else |
| 176 | data->pwm[1] = 0; |
| 177 | |
Antonio Ospite | 07172d2 | 2009-06-19 13:53:07 +0200 | [diff] [blame] | 178 | schedule_work(&data->work); |
Riku Voipio | 934cd3f | 2008-12-03 08:21:36 +0000 | [diff] [blame] | 179 | |
Antonio Ospite | 07172d2 | 2009-06-19 13:53:07 +0200 | [diff] [blame] | 180 | return 0; |
Riku Voipio | 934cd3f | 2008-12-03 08:21:36 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | static void pca9532_input_work(struct work_struct *work) |
| 184 | { |
Antonio Ospite | 07172d2 | 2009-06-19 13:53:07 +0200 | [diff] [blame] | 185 | struct pca9532_data *data; |
| 186 | data = container_of(work, struct pca9532_data, work); |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 187 | mutex_lock(&data->update_lock); |
| 188 | i2c_smbus_write_byte_data(data->client, PCA9532_REG_PWM(1), |
| 189 | data->pwm[1]); |
| 190 | mutex_unlock(&data->update_lock); |
Riku Voipio | 934cd3f | 2008-12-03 08:21:36 +0000 | [diff] [blame] | 191 | } |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 192 | |
Riku Voipio | 934cd3f | 2008-12-03 08:21:36 +0000 | [diff] [blame] | 193 | static void pca9532_led_work(struct work_struct *work) |
| 194 | { |
Antonio Ospite | 07172d2 | 2009-06-19 13:53:07 +0200 | [diff] [blame] | 195 | struct pca9532_led *led; |
| 196 | led = container_of(work, struct pca9532_led, work); |
| 197 | if (led->state == PCA9532_PWM0) |
| 198 | pca9532_setpwm(led->client, 0); |
| 199 | pca9532_setled(led); |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | static int pca9532_configure(struct i2c_client *client, |
| 203 | struct pca9532_data *data, struct pca9532_platform_data *pdata) |
| 204 | { |
| 205 | int i, err = 0; |
| 206 | |
| 207 | for (i = 0; i < 2; i++) { |
| 208 | data->pwm[i] = pdata->pwm[i]; |
| 209 | data->psc[i] = pdata->psc[i]; |
| 210 | i2c_smbus_write_byte_data(client, PCA9532_REG_PWM(i), |
| 211 | data->pwm[i]); |
| 212 | i2c_smbus_write_byte_data(client, PCA9532_REG_PSC(i), |
| 213 | data->psc[i]); |
| 214 | } |
| 215 | |
| 216 | for (i = 0; i < 16; i++) { |
| 217 | struct pca9532_led *led = &data->leds[i]; |
| 218 | struct pca9532_led *pled = &pdata->leds[i]; |
| 219 | led->client = client; |
| 220 | led->id = i; |
| 221 | led->type = pled->type; |
| 222 | switch (led->type) { |
| 223 | case PCA9532_TYPE_NONE: |
| 224 | break; |
| 225 | case PCA9532_TYPE_LED: |
| 226 | led->state = pled->state; |
Richard Purdie | 85c5204 | 2009-09-07 14:35:04 +0100 | [diff] [blame] | 227 | led->name = pled->name; |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 228 | led->ldev.name = led->name; |
| 229 | led->ldev.brightness = LED_OFF; |
| 230 | led->ldev.brightness_set = pca9532_set_brightness; |
| 231 | led->ldev.blink_set = pca9532_set_blink; |
Antonio Ospite | 07172d2 | 2009-06-19 13:53:07 +0200 | [diff] [blame] | 232 | INIT_WORK(&led->work, pca9532_led_work); |
Sven Wegener | f785d02 | 2008-12-03 08:12:53 +0000 | [diff] [blame] | 233 | err = led_classdev_register(&client->dev, &led->ldev); |
| 234 | if (err < 0) { |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 235 | dev_err(&client->dev, |
| 236 | "couldn't register LED %s\n", |
| 237 | led->name); |
| 238 | goto exit; |
| 239 | } |
| 240 | pca9532_setled(led); |
| 241 | break; |
| 242 | case PCA9532_TYPE_N2100_BEEP: |
| 243 | BUG_ON(data->idev); |
| 244 | led->state = PCA9532_PWM1; |
| 245 | pca9532_setled(led); |
| 246 | data->idev = input_allocate_device(); |
| 247 | if (data->idev == NULL) { |
| 248 | err = -ENOMEM; |
| 249 | goto exit; |
| 250 | } |
| 251 | data->idev->name = pled->name; |
| 252 | data->idev->phys = "i2c/pca9532"; |
| 253 | data->idev->id.bustype = BUS_HOST; |
Richard Purdie | 85c5204 | 2009-09-07 14:35:04 +0100 | [diff] [blame] | 254 | data->idev->id.vendor = 0x001f; |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 255 | data->idev->id.product = 0x0001; |
| 256 | data->idev->id.version = 0x0100; |
| 257 | data->idev->evbit[0] = BIT_MASK(EV_SND); |
| 258 | data->idev->sndbit[0] = BIT_MASK(SND_BELL) | |
| 259 | BIT_MASK(SND_TONE); |
| 260 | data->idev->event = pca9532_event; |
| 261 | input_set_drvdata(data->idev, data); |
Antonio Ospite | 07172d2 | 2009-06-19 13:53:07 +0200 | [diff] [blame] | 262 | INIT_WORK(&data->work, pca9532_input_work); |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 263 | err = input_register_device(data->idev); |
| 264 | if (err) { |
| 265 | input_free_device(data->idev); |
Antonio Ospite | 07172d2 | 2009-06-19 13:53:07 +0200 | [diff] [blame] | 266 | cancel_work_sync(&data->work); |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 267 | data->idev = NULL; |
| 268 | goto exit; |
| 269 | } |
| 270 | break; |
| 271 | } |
| 272 | } |
| 273 | return 0; |
| 274 | |
| 275 | exit: |
| 276 | if (i > 0) |
| 277 | for (i = i - 1; i >= 0; i--) |
| 278 | switch (data->leds[i].type) { |
| 279 | case PCA9532_TYPE_NONE: |
| 280 | break; |
| 281 | case PCA9532_TYPE_LED: |
| 282 | led_classdev_unregister(&data->leds[i].ldev); |
Antonio Ospite | 07172d2 | 2009-06-19 13:53:07 +0200 | [diff] [blame] | 283 | cancel_work_sync(&data->leds[i].work); |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 284 | break; |
| 285 | case PCA9532_TYPE_N2100_BEEP: |
| 286 | if (data->idev != NULL) { |
| 287 | input_unregister_device(data->idev); |
| 288 | input_free_device(data->idev); |
Antonio Ospite | 07172d2 | 2009-06-19 13:53:07 +0200 | [diff] [blame] | 289 | cancel_work_sync(&data->work); |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 290 | data->idev = NULL; |
| 291 | } |
| 292 | break; |
| 293 | } |
| 294 | |
| 295 | return err; |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | static int pca9532_probe(struct i2c_client *client, |
| 299 | const struct i2c_device_id *id) |
| 300 | { |
| 301 | struct pca9532_data *data = i2c_get_clientdata(client); |
| 302 | struct pca9532_platform_data *pca9532_pdata = client->dev.platform_data; |
Sven Wegener | f785d02 | 2008-12-03 08:12:53 +0000 | [diff] [blame] | 303 | int err; |
| 304 | |
| 305 | if (!pca9532_pdata) |
| 306 | return -EIO; |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 307 | |
| 308 | if (!i2c_check_functionality(client->adapter, |
| 309 | I2C_FUNC_SMBUS_BYTE_DATA)) |
| 310 | return -EIO; |
| 311 | |
Sven Wegener | f785d02 | 2008-12-03 08:12:53 +0000 | [diff] [blame] | 312 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 313 | if (!data) |
| 314 | return -ENOMEM; |
| 315 | |
| 316 | dev_info(&client->dev, "setting platform data\n"); |
| 317 | i2c_set_clientdata(client, data); |
| 318 | data->client = client; |
| 319 | mutex_init(&data->update_lock); |
| 320 | |
Sven Wegener | f785d02 | 2008-12-03 08:12:53 +0000 | [diff] [blame] | 321 | err = pca9532_configure(client, data, pca9532_pdata); |
| 322 | if (err) { |
| 323 | kfree(data); |
| 324 | i2c_set_clientdata(client, NULL); |
| 325 | } |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 326 | |
Sven Wegener | f785d02 | 2008-12-03 08:12:53 +0000 | [diff] [blame] | 327 | return err; |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | static int pca9532_remove(struct i2c_client *client) |
| 331 | { |
| 332 | struct pca9532_data *data = i2c_get_clientdata(client); |
| 333 | int i; |
| 334 | for (i = 0; i < 16; i++) |
| 335 | switch (data->leds[i].type) { |
| 336 | case PCA9532_TYPE_NONE: |
| 337 | break; |
| 338 | case PCA9532_TYPE_LED: |
| 339 | led_classdev_unregister(&data->leds[i].ldev); |
Antonio Ospite | 07172d2 | 2009-06-19 13:53:07 +0200 | [diff] [blame] | 340 | cancel_work_sync(&data->leds[i].work); |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 341 | break; |
| 342 | case PCA9532_TYPE_N2100_BEEP: |
| 343 | if (data->idev != NULL) { |
| 344 | input_unregister_device(data->idev); |
| 345 | input_free_device(data->idev); |
Antonio Ospite | 07172d2 | 2009-06-19 13:53:07 +0200 | [diff] [blame] | 346 | cancel_work_sync(&data->work); |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 347 | data->idev = NULL; |
| 348 | } |
| 349 | break; |
| 350 | } |
| 351 | |
| 352 | kfree(data); |
| 353 | i2c_set_clientdata(client, NULL); |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | static int __init pca9532_init(void) |
| 358 | { |
| 359 | return i2c_add_driver(&pca9532_driver); |
| 360 | } |
| 361 | |
| 362 | static void __exit pca9532_exit(void) |
| 363 | { |
| 364 | i2c_del_driver(&pca9532_driver); |
| 365 | } |
| 366 | |
Riku Voipio | b26e0ed | 2009-03-03 21:37:17 +0200 | [diff] [blame] | 367 | MODULE_AUTHOR("Riku Voipio"); |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 368 | MODULE_LICENSE("GPL"); |
| 369 | MODULE_DESCRIPTION("PCA 9532 LED dimmer"); |
| 370 | |
| 371 | module_init(pca9532_init); |
| 372 | module_exit(pca9532_exit); |
| 373 | |