Paul Parsons | 7d9e7e9 | 2011-05-13 18:52:56 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 Paul Parsons <lost.distance@yahoo.com> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/kernel.h> |
Paul Parsons | 7d9e7e9 | 2011-05-13 18:52:56 +0000 | [diff] [blame] | 10 | #include <linux/platform_device.h> |
| 11 | #include <linux/leds.h> |
| 12 | #include <linux/slab.h> |
| 13 | |
| 14 | #include <linux/mfd/asic3.h> |
| 15 | #include <linux/mfd/core.h> |
Paul Gortmaker | 54f4ded | 2011-07-03 13:56:03 -0400 | [diff] [blame] | 16 | #include <linux/module.h> |
Paul Parsons | 7d9e7e9 | 2011-05-13 18:52:56 +0000 | [diff] [blame] | 17 | |
| 18 | /* |
| 19 | * The HTC ASIC3 LED GPIOs are inputs, not outputs. |
| 20 | * Hence we turn the LEDs on/off via the TimeBase register. |
| 21 | */ |
| 22 | |
| 23 | /* |
| 24 | * When TimeBase is 4 the clock resolution is about 32Hz. |
| 25 | * This driver supports hardware blinking with an on+off |
| 26 | * period from 62ms (2 clocks) to 125s (4000 clocks). |
| 27 | */ |
| 28 | #define MS_TO_CLK(ms) DIV_ROUND_CLOSEST(((ms)*1024), 32000) |
| 29 | #define CLK_TO_MS(clk) (((clk)*32000)/1024) |
| 30 | #define MAX_CLK 4000 /* Fits into 12-bit Time registers */ |
| 31 | #define MAX_MS CLK_TO_MS(MAX_CLK) |
| 32 | |
| 33 | static const unsigned int led_n_base[ASIC3_NUM_LEDS] = { |
| 34 | [0] = ASIC3_LED_0_Base, |
| 35 | [1] = ASIC3_LED_1_Base, |
| 36 | [2] = ASIC3_LED_2_Base, |
| 37 | }; |
| 38 | |
| 39 | static void brightness_set(struct led_classdev *cdev, |
| 40 | enum led_brightness value) |
| 41 | { |
| 42 | struct platform_device *pdev = to_platform_device(cdev->dev->parent); |
| 43 | const struct mfd_cell *cell = mfd_get_cell(pdev); |
| 44 | struct asic3 *asic = dev_get_drvdata(pdev->dev.parent); |
| 45 | u32 timebase; |
| 46 | unsigned int base; |
| 47 | |
| 48 | timebase = (value == LED_OFF) ? 0 : (LED_EN|0x4); |
| 49 | |
| 50 | base = led_n_base[cell->id]; |
| 51 | asic3_write_register(asic, (base + ASIC3_LED_PeriodTime), 32); |
| 52 | asic3_write_register(asic, (base + ASIC3_LED_DutyTime), 32); |
| 53 | asic3_write_register(asic, (base + ASIC3_LED_AutoStopCount), 0); |
| 54 | asic3_write_register(asic, (base + ASIC3_LED_TimeBase), timebase); |
| 55 | } |
| 56 | |
| 57 | static int blink_set(struct led_classdev *cdev, |
| 58 | unsigned long *delay_on, |
| 59 | unsigned long *delay_off) |
| 60 | { |
| 61 | struct platform_device *pdev = to_platform_device(cdev->dev->parent); |
| 62 | const struct mfd_cell *cell = mfd_get_cell(pdev); |
| 63 | struct asic3 *asic = dev_get_drvdata(pdev->dev.parent); |
| 64 | u32 on; |
| 65 | u32 off; |
| 66 | unsigned int base; |
| 67 | |
| 68 | if (*delay_on > MAX_MS || *delay_off > MAX_MS) |
| 69 | return -EINVAL; |
| 70 | |
| 71 | if (*delay_on == 0 && *delay_off == 0) { |
| 72 | /* If both are zero then a sensible default should be chosen */ |
| 73 | on = MS_TO_CLK(500); |
| 74 | off = MS_TO_CLK(500); |
| 75 | } else { |
| 76 | on = MS_TO_CLK(*delay_on); |
| 77 | off = MS_TO_CLK(*delay_off); |
| 78 | if ((on + off) > MAX_CLK) |
| 79 | return -EINVAL; |
| 80 | } |
| 81 | |
| 82 | base = led_n_base[cell->id]; |
| 83 | asic3_write_register(asic, (base + ASIC3_LED_PeriodTime), (on + off)); |
| 84 | asic3_write_register(asic, (base + ASIC3_LED_DutyTime), on); |
| 85 | asic3_write_register(asic, (base + ASIC3_LED_AutoStopCount), 0); |
| 86 | asic3_write_register(asic, (base + ASIC3_LED_TimeBase), (LED_EN|0x4)); |
| 87 | |
| 88 | *delay_on = CLK_TO_MS(on); |
| 89 | *delay_off = CLK_TO_MS(off); |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
Bill Pemberton | 98ea1ea | 2012-11-19 13:23:02 -0500 | [diff] [blame] | 94 | static int asic3_led_probe(struct platform_device *pdev) |
Paul Parsons | 7d9e7e9 | 2011-05-13 18:52:56 +0000 | [diff] [blame] | 95 | { |
Jingoo Han | 87aae1e | 2013-07-30 01:07:35 -0700 | [diff] [blame] | 96 | struct asic3_led *led = dev_get_platdata(&pdev->dev); |
Paul Parsons | 7d9e7e9 | 2011-05-13 18:52:56 +0000 | [diff] [blame] | 97 | int ret; |
| 98 | |
| 99 | ret = mfd_cell_enable(pdev); |
| 100 | if (ret < 0) |
Bryan Wu | 60a0aad | 2012-07-03 12:47:16 +0800 | [diff] [blame] | 101 | return ret; |
Paul Parsons | 7d9e7e9 | 2011-05-13 18:52:56 +0000 | [diff] [blame] | 102 | |
Bryan Wu | 60a0aad | 2012-07-03 12:47:16 +0800 | [diff] [blame] | 103 | led->cdev = devm_kzalloc(&pdev->dev, sizeof(struct led_classdev), |
| 104 | GFP_KERNEL); |
Paul Parsons | 7d9e7e9 | 2011-05-13 18:52:56 +0000 | [diff] [blame] | 105 | if (!led->cdev) { |
| 106 | ret = -ENOMEM; |
Bryan Wu | 60a0aad | 2012-07-03 12:47:16 +0800 | [diff] [blame] | 107 | goto out; |
Paul Parsons | 7d9e7e9 | 2011-05-13 18:52:56 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | led->cdev->name = led->name; |
Paul Parsons | e0b13b5 | 2011-08-09 16:27:33 +0000 | [diff] [blame] | 111 | led->cdev->flags = LED_CORE_SUSPENDRESUME; |
Paul Parsons | 7d9e7e9 | 2011-05-13 18:52:56 +0000 | [diff] [blame] | 112 | led->cdev->brightness_set = brightness_set; |
| 113 | led->cdev->blink_set = blink_set; |
Paul Parsons | e0b13b5 | 2011-08-09 16:27:33 +0000 | [diff] [blame] | 114 | led->cdev->default_trigger = led->default_trigger; |
Paul Parsons | 7d9e7e9 | 2011-05-13 18:52:56 +0000 | [diff] [blame] | 115 | |
| 116 | ret = led_classdev_register(&pdev->dev, led->cdev); |
| 117 | if (ret < 0) |
Bryan Wu | 60a0aad | 2012-07-03 12:47:16 +0800 | [diff] [blame] | 118 | goto out; |
Paul Parsons | 7d9e7e9 | 2011-05-13 18:52:56 +0000 | [diff] [blame] | 119 | |
| 120 | return 0; |
| 121 | |
Bryan Wu | 60a0aad | 2012-07-03 12:47:16 +0800 | [diff] [blame] | 122 | out: |
Paul Parsons | 7d9e7e9 | 2011-05-13 18:52:56 +0000 | [diff] [blame] | 123 | (void) mfd_cell_disable(pdev); |
Paul Parsons | 7d9e7e9 | 2011-05-13 18:52:56 +0000 | [diff] [blame] | 124 | return ret; |
| 125 | } |
| 126 | |
Bill Pemberton | 678e8a6 | 2012-11-19 13:26:00 -0500 | [diff] [blame] | 127 | static int asic3_led_remove(struct platform_device *pdev) |
Paul Parsons | 7d9e7e9 | 2011-05-13 18:52:56 +0000 | [diff] [blame] | 128 | { |
Jingoo Han | 87aae1e | 2013-07-30 01:07:35 -0700 | [diff] [blame] | 129 | struct asic3_led *led = dev_get_platdata(&pdev->dev); |
Paul Parsons | 7d9e7e9 | 2011-05-13 18:52:56 +0000 | [diff] [blame] | 130 | |
| 131 | led_classdev_unregister(led->cdev); |
| 132 | |
Paul Parsons | 7d9e7e9 | 2011-05-13 18:52:56 +0000 | [diff] [blame] | 133 | return mfd_cell_disable(pdev); |
| 134 | } |
| 135 | |
Jingoo Han | df92d5f | 2013-03-25 23:47:19 -0700 | [diff] [blame] | 136 | #ifdef CONFIG_PM_SLEEP |
Paul Parsons | e0b13b5 | 2011-08-09 16:27:33 +0000 | [diff] [blame] | 137 | static int asic3_led_suspend(struct device *dev) |
| 138 | { |
| 139 | struct platform_device *pdev = to_platform_device(dev); |
| 140 | const struct mfd_cell *cell = mfd_get_cell(pdev); |
| 141 | int ret; |
| 142 | |
| 143 | ret = 0; |
| 144 | if (cell->suspend) |
| 145 | ret = (*cell->suspend)(pdev); |
| 146 | |
| 147 | return ret; |
| 148 | } |
| 149 | |
| 150 | static int asic3_led_resume(struct device *dev) |
| 151 | { |
| 152 | struct platform_device *pdev = to_platform_device(dev); |
| 153 | const struct mfd_cell *cell = mfd_get_cell(pdev); |
| 154 | int ret; |
| 155 | |
| 156 | ret = 0; |
| 157 | if (cell->resume) |
| 158 | ret = (*cell->resume)(pdev); |
| 159 | |
| 160 | return ret; |
| 161 | } |
Jingoo Han | df92d5f | 2013-03-25 23:47:19 -0700 | [diff] [blame] | 162 | #endif |
Paul Parsons | e0b13b5 | 2011-08-09 16:27:33 +0000 | [diff] [blame] | 163 | |
Jingoo Han | df92d5f | 2013-03-25 23:47:19 -0700 | [diff] [blame] | 164 | static SIMPLE_DEV_PM_OPS(asic3_led_pm_ops, asic3_led_suspend, asic3_led_resume); |
Paul Parsons | e0b13b5 | 2011-08-09 16:27:33 +0000 | [diff] [blame] | 165 | |
Paul Parsons | 7d9e7e9 | 2011-05-13 18:52:56 +0000 | [diff] [blame] | 166 | static struct platform_driver asic3_led_driver = { |
| 167 | .probe = asic3_led_probe, |
Bill Pemberton | df07cf8 | 2012-11-19 13:20:20 -0500 | [diff] [blame] | 168 | .remove = asic3_led_remove, |
Paul Parsons | 7d9e7e9 | 2011-05-13 18:52:56 +0000 | [diff] [blame] | 169 | .driver = { |
| 170 | .name = "leds-asic3", |
| 171 | .owner = THIS_MODULE, |
Paul Parsons | e0b13b5 | 2011-08-09 16:27:33 +0000 | [diff] [blame] | 172 | .pm = &asic3_led_pm_ops, |
Paul Parsons | 7d9e7e9 | 2011-05-13 18:52:56 +0000 | [diff] [blame] | 173 | }, |
| 174 | }; |
| 175 | |
Axel Lin | 892a884 | 2012-01-10 15:09:24 -0800 | [diff] [blame] | 176 | module_platform_driver(asic3_led_driver); |
Paul Parsons | 7d9e7e9 | 2011-05-13 18:52:56 +0000 | [diff] [blame] | 177 | |
| 178 | MODULE_AUTHOR("Paul Parsons <lost.distance@yahoo.com>"); |
| 179 | MODULE_DESCRIPTION("HTC ASIC3 LED driver"); |
| 180 | MODULE_LICENSE("GPL"); |
Axel Lin | 892a884 | 2012-01-10 15:09:24 -0800 | [diff] [blame] | 181 | MODULE_ALIAS("platform:leds-asic3"); |