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