Guennadi Liakhovetski | ac2dd0f | 2009-01-10 18:58:28 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008 |
| 3 | * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de> |
| 4 | * |
| 5 | * This file is subject to the terms and conditions of version 2 of |
| 6 | * the GNU General Public License. See the file COPYING in the main |
| 7 | * directory of this archive for more details. |
| 8 | * |
| 9 | * LED driver for the DAC124S085 SPI DAC |
| 10 | */ |
| 11 | |
Guennadi Liakhovetski | ac2dd0f | 2009-01-10 18:58:28 +0000 | [diff] [blame] | 12 | #include <linux/leds.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/mutex.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/spinlock.h> |
| 17 | #include <linux/workqueue.h> |
| 18 | #include <linux/spi/spi.h> |
| 19 | |
| 20 | struct dac124s085_led { |
| 21 | struct led_classdev ldev; |
| 22 | struct spi_device *spi; |
| 23 | int id; |
| 24 | int brightness; |
| 25 | char name[sizeof("dac124s085-3")]; |
| 26 | |
| 27 | struct mutex mutex; |
| 28 | struct work_struct work; |
| 29 | spinlock_t lock; |
| 30 | }; |
| 31 | |
| 32 | struct dac124s085 { |
| 33 | struct dac124s085_led leds[4]; |
| 34 | }; |
| 35 | |
| 36 | #define REG_WRITE (0 << 12) |
| 37 | #define REG_WRITE_UPDATE (1 << 12) |
| 38 | #define ALL_WRITE_UPDATE (2 << 12) |
| 39 | #define POWER_DOWN_OUTPUT (3 << 12) |
| 40 | |
| 41 | static void dac124s085_led_work(struct work_struct *work) |
| 42 | { |
| 43 | struct dac124s085_led *led = container_of(work, struct dac124s085_led, |
| 44 | work); |
| 45 | u16 word; |
| 46 | |
| 47 | mutex_lock(&led->mutex); |
| 48 | word = cpu_to_le16(((led->id) << 14) | REG_WRITE_UPDATE | |
| 49 | (led->brightness & 0xfff)); |
| 50 | spi_write(led->spi, (const u8 *)&word, sizeof(word)); |
| 51 | mutex_unlock(&led->mutex); |
| 52 | } |
| 53 | |
| 54 | static void dac124s085_set_brightness(struct led_classdev *ldev, |
| 55 | enum led_brightness brightness) |
| 56 | { |
| 57 | struct dac124s085_led *led = container_of(ldev, struct dac124s085_led, |
| 58 | ldev); |
| 59 | |
| 60 | spin_lock(&led->lock); |
| 61 | led->brightness = brightness; |
| 62 | schedule_work(&led->work); |
| 63 | spin_unlock(&led->lock); |
| 64 | } |
| 65 | |
| 66 | static int dac124s085_probe(struct spi_device *spi) |
| 67 | { |
| 68 | struct dac124s085 *dac; |
| 69 | struct dac124s085_led *led; |
| 70 | int i, ret; |
| 71 | |
Bryan Wu | e586031 | 2012-07-04 12:27:40 +0800 | [diff] [blame] | 72 | dac = devm_kzalloc(&spi->dev, sizeof(*dac), GFP_KERNEL); |
Guennadi Liakhovetski | ac2dd0f | 2009-01-10 18:58:28 +0000 | [diff] [blame] | 73 | if (!dac) |
| 74 | return -ENOMEM; |
| 75 | |
| 76 | spi->bits_per_word = 16; |
| 77 | |
| 78 | for (i = 0; i < ARRAY_SIZE(dac->leds); i++) { |
| 79 | led = dac->leds + i; |
| 80 | led->id = i; |
| 81 | led->brightness = LED_OFF; |
| 82 | led->spi = spi; |
| 83 | snprintf(led->name, sizeof(led->name), "dac124s085-%d", i); |
| 84 | spin_lock_init(&led->lock); |
| 85 | INIT_WORK(&led->work, dac124s085_led_work); |
| 86 | mutex_init(&led->mutex); |
| 87 | led->ldev.name = led->name; |
| 88 | led->ldev.brightness = LED_OFF; |
| 89 | led->ldev.max_brightness = 0xfff; |
| 90 | led->ldev.brightness_set = dac124s085_set_brightness; |
| 91 | ret = led_classdev_register(&spi->dev, &led->ldev); |
| 92 | if (ret < 0) |
| 93 | goto eledcr; |
| 94 | } |
| 95 | |
| 96 | spi_set_drvdata(spi, dac); |
| 97 | |
| 98 | return 0; |
| 99 | |
| 100 | eledcr: |
| 101 | while (i--) |
| 102 | led_classdev_unregister(&dac->leds[i].ldev); |
| 103 | |
| 104 | spi_set_drvdata(spi, NULL); |
Guennadi Liakhovetski | ac2dd0f | 2009-01-10 18:58:28 +0000 | [diff] [blame] | 105 | return ret; |
| 106 | } |
| 107 | |
| 108 | static int dac124s085_remove(struct spi_device *spi) |
| 109 | { |
| 110 | struct dac124s085 *dac = spi_get_drvdata(spi); |
| 111 | int i; |
| 112 | |
| 113 | for (i = 0; i < ARRAY_SIZE(dac->leds); i++) { |
| 114 | led_classdev_unregister(&dac->leds[i].ldev); |
| 115 | cancel_work_sync(&dac->leds[i].work); |
| 116 | } |
| 117 | |
| 118 | spi_set_drvdata(spi, NULL); |
Guennadi Liakhovetski | ac2dd0f | 2009-01-10 18:58:28 +0000 | [diff] [blame] | 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | static struct spi_driver dac124s085_driver = { |
| 124 | .probe = dac124s085_probe, |
| 125 | .remove = dac124s085_remove, |
| 126 | .driver = { |
| 127 | .name = "dac124s085", |
| 128 | .owner = THIS_MODULE, |
| 129 | }, |
| 130 | }; |
| 131 | |
Axel Lin | 19b9557 | 2012-01-10 15:09:30 -0800 | [diff] [blame] | 132 | module_spi_driver(dac124s085_driver); |
Guennadi Liakhovetski | ac2dd0f | 2009-01-10 18:58:28 +0000 | [diff] [blame] | 133 | |
| 134 | MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>"); |
| 135 | MODULE_DESCRIPTION("DAC124S085 LED driver"); |
| 136 | MODULE_LICENSE("GPL v2"); |
Anton Vorontsov | e0626e3 | 2009-09-22 16:46:08 -0700 | [diff] [blame] | 137 | MODULE_ALIAS("spi:dac124s085"); |