blob: 8cbc1b8bafa5a84aeda32e0ce0f0e72de96c04b0 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Paul Parsons7d9e7e92011-05-13 18:52:56 +00002/*
3 * Copyright (C) 2011 Paul Parsons <lost.distance@yahoo.com>
Paul Parsons7d9e7e92011-05-13 18:52:56 +00004 */
5
6#include <linux/kernel.h>
Paul Parsons7d9e7e92011-05-13 18:52:56 +00007#include <linux/platform_device.h>
8#include <linux/leds.h>
9#include <linux/slab.h>
10
11#include <linux/mfd/asic3.h>
12#include <linux/mfd/core.h>
Paul Gortmaker54f4ded2011-07-03 13:56:03 -040013#include <linux/module.h>
Paul Parsons7d9e7e92011-05-13 18:52:56 +000014
15/*
16 * The HTC ASIC3 LED GPIOs are inputs, not outputs.
17 * Hence we turn the LEDs on/off via the TimeBase register.
18 */
19
20/*
21 * When TimeBase is 4 the clock resolution is about 32Hz.
22 * This driver supports hardware blinking with an on+off
23 * period from 62ms (2 clocks) to 125s (4000 clocks).
24 */
25#define MS_TO_CLK(ms) DIV_ROUND_CLOSEST(((ms)*1024), 32000)
26#define CLK_TO_MS(clk) (((clk)*32000)/1024)
27#define MAX_CLK 4000 /* Fits into 12-bit Time registers */
28#define MAX_MS CLK_TO_MS(MAX_CLK)
29
30static const unsigned int led_n_base[ASIC3_NUM_LEDS] = {
31 [0] = ASIC3_LED_0_Base,
32 [1] = ASIC3_LED_1_Base,
33 [2] = ASIC3_LED_2_Base,
34};
35
36static void brightness_set(struct led_classdev *cdev,
37 enum led_brightness value)
38{
39 struct platform_device *pdev = to_platform_device(cdev->dev->parent);
40 const struct mfd_cell *cell = mfd_get_cell(pdev);
41 struct asic3 *asic = dev_get_drvdata(pdev->dev.parent);
42 u32 timebase;
43 unsigned int base;
44
45 timebase = (value == LED_OFF) ? 0 : (LED_EN|0x4);
46
47 base = led_n_base[cell->id];
48 asic3_write_register(asic, (base + ASIC3_LED_PeriodTime), 32);
49 asic3_write_register(asic, (base + ASIC3_LED_DutyTime), 32);
50 asic3_write_register(asic, (base + ASIC3_LED_AutoStopCount), 0);
51 asic3_write_register(asic, (base + ASIC3_LED_TimeBase), timebase);
52}
53
54static int blink_set(struct led_classdev *cdev,
55 unsigned long *delay_on,
56 unsigned long *delay_off)
57{
58 struct platform_device *pdev = to_platform_device(cdev->dev->parent);
59 const struct mfd_cell *cell = mfd_get_cell(pdev);
60 struct asic3 *asic = dev_get_drvdata(pdev->dev.parent);
61 u32 on;
62 u32 off;
63 unsigned int base;
64
65 if (*delay_on > MAX_MS || *delay_off > MAX_MS)
66 return -EINVAL;
67
68 if (*delay_on == 0 && *delay_off == 0) {
69 /* If both are zero then a sensible default should be chosen */
70 on = MS_TO_CLK(500);
71 off = MS_TO_CLK(500);
72 } else {
73 on = MS_TO_CLK(*delay_on);
74 off = MS_TO_CLK(*delay_off);
75 if ((on + off) > MAX_CLK)
76 return -EINVAL;
77 }
78
79 base = led_n_base[cell->id];
80 asic3_write_register(asic, (base + ASIC3_LED_PeriodTime), (on + off));
81 asic3_write_register(asic, (base + ASIC3_LED_DutyTime), on);
82 asic3_write_register(asic, (base + ASIC3_LED_AutoStopCount), 0);
83 asic3_write_register(asic, (base + ASIC3_LED_TimeBase), (LED_EN|0x4));
84
85 *delay_on = CLK_TO_MS(on);
86 *delay_off = CLK_TO_MS(off);
87
88 return 0;
89}
90
Bill Pemberton98ea1ea2012-11-19 13:23:02 -050091static int asic3_led_probe(struct platform_device *pdev)
Paul Parsons7d9e7e92011-05-13 18:52:56 +000092{
Jingoo Han87aae1e2013-07-30 01:07:35 -070093 struct asic3_led *led = dev_get_platdata(&pdev->dev);
Paul Parsons7d9e7e92011-05-13 18:52:56 +000094 int ret;
95
96 ret = mfd_cell_enable(pdev);
97 if (ret < 0)
Bryan Wu60a0aad2012-07-03 12:47:16 +080098 return ret;
Paul Parsons7d9e7e92011-05-13 18:52:56 +000099
Bryan Wu60a0aad2012-07-03 12:47:16 +0800100 led->cdev = devm_kzalloc(&pdev->dev, sizeof(struct led_classdev),
101 GFP_KERNEL);
Paul Parsons7d9e7e92011-05-13 18:52:56 +0000102 if (!led->cdev) {
103 ret = -ENOMEM;
Bryan Wu60a0aad2012-07-03 12:47:16 +0800104 goto out;
Paul Parsons7d9e7e92011-05-13 18:52:56 +0000105 }
106
107 led->cdev->name = led->name;
Paul Parsonse0b13b52011-08-09 16:27:33 +0000108 led->cdev->flags = LED_CORE_SUSPENDRESUME;
Paul Parsons7d9e7e92011-05-13 18:52:56 +0000109 led->cdev->brightness_set = brightness_set;
110 led->cdev->blink_set = blink_set;
Paul Parsonse0b13b52011-08-09 16:27:33 +0000111 led->cdev->default_trigger = led->default_trigger;
Paul Parsons7d9e7e92011-05-13 18:52:56 +0000112
113 ret = led_classdev_register(&pdev->dev, led->cdev);
114 if (ret < 0)
Bryan Wu60a0aad2012-07-03 12:47:16 +0800115 goto out;
Paul Parsons7d9e7e92011-05-13 18:52:56 +0000116
117 return 0;
118
Bryan Wu60a0aad2012-07-03 12:47:16 +0800119out:
Paul Parsons7d9e7e92011-05-13 18:52:56 +0000120 (void) mfd_cell_disable(pdev);
Paul Parsons7d9e7e92011-05-13 18:52:56 +0000121 return ret;
122}
123
Bill Pemberton678e8a62012-11-19 13:26:00 -0500124static int asic3_led_remove(struct platform_device *pdev)
Paul Parsons7d9e7e92011-05-13 18:52:56 +0000125{
Jingoo Han87aae1e2013-07-30 01:07:35 -0700126 struct asic3_led *led = dev_get_platdata(&pdev->dev);
Paul Parsons7d9e7e92011-05-13 18:52:56 +0000127
128 led_classdev_unregister(led->cdev);
129
Paul Parsons7d9e7e92011-05-13 18:52:56 +0000130 return mfd_cell_disable(pdev);
131}
132
Jingoo Handf92d5f2013-03-25 23:47:19 -0700133#ifdef CONFIG_PM_SLEEP
Paul Parsonse0b13b52011-08-09 16:27:33 +0000134static int asic3_led_suspend(struct device *dev)
135{
136 struct platform_device *pdev = to_platform_device(dev);
137 const struct mfd_cell *cell = mfd_get_cell(pdev);
138 int ret;
139
140 ret = 0;
141 if (cell->suspend)
142 ret = (*cell->suspend)(pdev);
143
144 return ret;
145}
146
147static int asic3_led_resume(struct device *dev)
148{
149 struct platform_device *pdev = to_platform_device(dev);
150 const struct mfd_cell *cell = mfd_get_cell(pdev);
151 int ret;
152
153 ret = 0;
154 if (cell->resume)
155 ret = (*cell->resume)(pdev);
156
157 return ret;
158}
Jingoo Handf92d5f2013-03-25 23:47:19 -0700159#endif
Paul Parsonse0b13b52011-08-09 16:27:33 +0000160
Jingoo Handf92d5f2013-03-25 23:47:19 -0700161static SIMPLE_DEV_PM_OPS(asic3_led_pm_ops, asic3_led_suspend, asic3_led_resume);
Paul Parsonse0b13b52011-08-09 16:27:33 +0000162
Paul Parsons7d9e7e92011-05-13 18:52:56 +0000163static struct platform_driver asic3_led_driver = {
164 .probe = asic3_led_probe,
Bill Pembertondf07cf82012-11-19 13:20:20 -0500165 .remove = asic3_led_remove,
Paul Parsons7d9e7e92011-05-13 18:52:56 +0000166 .driver = {
167 .name = "leds-asic3",
Paul Parsonse0b13b52011-08-09 16:27:33 +0000168 .pm = &asic3_led_pm_ops,
Paul Parsons7d9e7e92011-05-13 18:52:56 +0000169 },
170};
171
Axel Lin892a8842012-01-10 15:09:24 -0800172module_platform_driver(asic3_led_driver);
Paul Parsons7d9e7e92011-05-13 18:52:56 +0000173
174MODULE_AUTHOR("Paul Parsons <lost.distance@yahoo.com>");
175MODULE_DESCRIPTION("HTC ASIC3 LED driver");
176MODULE_LICENSE("GPL");
Axel Lin892a8842012-01-10 15:09:24 -0800177MODULE_ALIAS("platform:leds-asic3");