Thomas Gleixner | 36edc93 | 2019-05-29 16:57:44 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Guennadi Liakhovetski | ac2dd0f | 2009-01-10 18:58:28 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2008 |
| 4 | * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de> |
| 5 | * |
Guennadi Liakhovetski | ac2dd0f | 2009-01-10 18:58:28 +0000 | [diff] [blame] | 6 | * LED driver for the DAC124S085 SPI DAC |
| 7 | */ |
| 8 | |
Guennadi Liakhovetski | ac2dd0f | 2009-01-10 18:58:28 +0000 | [diff] [blame] | 9 | #include <linux/leds.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/mutex.h> |
| 12 | #include <linux/slab.h> |
Guennadi Liakhovetski | ac2dd0f | 2009-01-10 18:58:28 +0000 | [diff] [blame] | 13 | #include <linux/spi/spi.h> |
| 14 | |
| 15 | struct dac124s085_led { |
| 16 | struct led_classdev ldev; |
| 17 | struct spi_device *spi; |
| 18 | int id; |
Guennadi Liakhovetski | ac2dd0f | 2009-01-10 18:58:28 +0000 | [diff] [blame] | 19 | char name[sizeof("dac124s085-3")]; |
| 20 | |
| 21 | struct mutex mutex; |
Guennadi Liakhovetski | ac2dd0f | 2009-01-10 18:58:28 +0000 | [diff] [blame] | 22 | }; |
| 23 | |
| 24 | struct dac124s085 { |
| 25 | struct dac124s085_led leds[4]; |
| 26 | }; |
| 27 | |
| 28 | #define REG_WRITE (0 << 12) |
| 29 | #define REG_WRITE_UPDATE (1 << 12) |
| 30 | #define ALL_WRITE_UPDATE (2 << 12) |
| 31 | #define POWER_DOWN_OUTPUT (3 << 12) |
| 32 | |
Andrew Lunn | 8ec108c | 2015-08-20 12:47:25 +0200 | [diff] [blame] | 33 | static int dac124s085_set_brightness(struct led_classdev *ldev, |
Guennadi Liakhovetski | ac2dd0f | 2009-01-10 18:58:28 +0000 | [diff] [blame] | 34 | enum led_brightness brightness) |
| 35 | { |
| 36 | struct dac124s085_led *led = container_of(ldev, struct dac124s085_led, |
| 37 | ldev); |
Andrew Lunn | 8ec108c | 2015-08-20 12:47:25 +0200 | [diff] [blame] | 38 | u16 word; |
| 39 | int ret; |
Guennadi Liakhovetski | ac2dd0f | 2009-01-10 18:58:28 +0000 | [diff] [blame] | 40 | |
Andrew Lunn | 8ec108c | 2015-08-20 12:47:25 +0200 | [diff] [blame] | 41 | mutex_lock(&led->mutex); |
| 42 | word = cpu_to_le16(((led->id) << 14) | REG_WRITE_UPDATE | |
| 43 | (brightness & 0xfff)); |
| 44 | ret = spi_write(led->spi, (const u8 *)&word, sizeof(word)); |
| 45 | mutex_unlock(&led->mutex); |
| 46 | |
| 47 | return ret; |
Guennadi Liakhovetski | ac2dd0f | 2009-01-10 18:58:28 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | static int dac124s085_probe(struct spi_device *spi) |
| 51 | { |
| 52 | struct dac124s085 *dac; |
| 53 | struct dac124s085_led *led; |
| 54 | int i, ret; |
| 55 | |
Bryan Wu | e586031 | 2012-07-04 12:27:40 +0800 | [diff] [blame] | 56 | dac = devm_kzalloc(&spi->dev, sizeof(*dac), GFP_KERNEL); |
Guennadi Liakhovetski | ac2dd0f | 2009-01-10 18:58:28 +0000 | [diff] [blame] | 57 | if (!dac) |
| 58 | return -ENOMEM; |
| 59 | |
| 60 | spi->bits_per_word = 16; |
| 61 | |
| 62 | for (i = 0; i < ARRAY_SIZE(dac->leds); i++) { |
| 63 | led = dac->leds + i; |
| 64 | led->id = i; |
Guennadi Liakhovetski | ac2dd0f | 2009-01-10 18:58:28 +0000 | [diff] [blame] | 65 | led->spi = spi; |
| 66 | snprintf(led->name, sizeof(led->name), "dac124s085-%d", i); |
Guennadi Liakhovetski | ac2dd0f | 2009-01-10 18:58:28 +0000 | [diff] [blame] | 67 | mutex_init(&led->mutex); |
| 68 | led->ldev.name = led->name; |
| 69 | led->ldev.brightness = LED_OFF; |
| 70 | led->ldev.max_brightness = 0xfff; |
Andrew Lunn | 8ec108c | 2015-08-20 12:47:25 +0200 | [diff] [blame] | 71 | led->ldev.brightness_set_blocking = dac124s085_set_brightness; |
Guennadi Liakhovetski | ac2dd0f | 2009-01-10 18:58:28 +0000 | [diff] [blame] | 72 | ret = led_classdev_register(&spi->dev, &led->ldev); |
| 73 | if (ret < 0) |
| 74 | goto eledcr; |
| 75 | } |
| 76 | |
| 77 | spi_set_drvdata(spi, dac); |
| 78 | |
| 79 | return 0; |
| 80 | |
| 81 | eledcr: |
| 82 | while (i--) |
| 83 | led_classdev_unregister(&dac->leds[i].ldev); |
| 84 | |
Guennadi Liakhovetski | ac2dd0f | 2009-01-10 18:58:28 +0000 | [diff] [blame] | 85 | return ret; |
| 86 | } |
| 87 | |
| 88 | static int dac124s085_remove(struct spi_device *spi) |
| 89 | { |
| 90 | struct dac124s085 *dac = spi_get_drvdata(spi); |
| 91 | int i; |
| 92 | |
Andrew Lunn | 8ec108c | 2015-08-20 12:47:25 +0200 | [diff] [blame] | 93 | for (i = 0; i < ARRAY_SIZE(dac->leds); i++) |
Guennadi Liakhovetski | ac2dd0f | 2009-01-10 18:58:28 +0000 | [diff] [blame] | 94 | led_classdev_unregister(&dac->leds[i].ldev); |
Guennadi Liakhovetski | ac2dd0f | 2009-01-10 18:58:28 +0000 | [diff] [blame] | 95 | |
Guennadi Liakhovetski | ac2dd0f | 2009-01-10 18:58:28 +0000 | [diff] [blame] | 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | static struct spi_driver dac124s085_driver = { |
| 100 | .probe = dac124s085_probe, |
| 101 | .remove = dac124s085_remove, |
| 102 | .driver = { |
| 103 | .name = "dac124s085", |
Guennadi Liakhovetski | ac2dd0f | 2009-01-10 18:58:28 +0000 | [diff] [blame] | 104 | }, |
| 105 | }; |
| 106 | |
Axel Lin | 19b9557 | 2012-01-10 15:09:30 -0800 | [diff] [blame] | 107 | module_spi_driver(dac124s085_driver); |
Guennadi Liakhovetski | ac2dd0f | 2009-01-10 18:58:28 +0000 | [diff] [blame] | 108 | |
| 109 | MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>"); |
| 110 | MODULE_DESCRIPTION("DAC124S085 LED driver"); |
| 111 | MODULE_LICENSE("GPL v2"); |
Anton Vorontsov | e0626e3 | 2009-09-22 16:46:08 -0700 | [diff] [blame] | 112 | MODULE_ALIAS("spi:dac124s085"); |