blob: 386da1566fccb41f76a6828e3e5de2dde965ccfb [file] [log] [blame]
Philipp Zabel5dc33392008-04-12 13:25:41 +01001/*
2 * Core driver for HTC PASIC3 LED/DS1WM chip.
3 *
4 * Copyright (C) 2006 Philipp Zabel <philipp.zabel@gmail.com>
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
11#include <linux/init.h>
12#include <linux/module.h>
13#include <linux/platform_device.h>
14
Philipp Zabel5dc33392008-04-12 13:25:41 +010015#include <linux/gpio.h>
16#include <linux/io.h>
17#include <linux/irq.h>
18#include <linux/interrupt.h>
Philipp Zabel0254a8f2009-02-17 10:06:45 +010019#include <linux/mfd/core.h>
20#include <linux/mfd/ds1wm.h>
Philipp Zabel5dc33392008-04-12 13:25:41 +010021#include <linux/mfd/htc-pasic3.h>
22
Philipp Zabel5dc33392008-04-12 13:25:41 +010023struct pasic3_data {
24 void __iomem *mapping;
25 unsigned int bus_shift;
Philipp Zabel5dc33392008-04-12 13:25:41 +010026};
27
28#define REG_ADDR 5
29#define REG_DATA 6
Philipp Zabel5dc33392008-04-12 13:25:41 +010030
31#define READ_MODE 0x80
32
33/*
34 * write to a secondary register on the PASIC3
35 */
36void pasic3_write_register(struct device *dev, u32 reg, u8 val)
37{
38 struct pasic3_data *asic = dev->driver_data;
39 int bus_shift = asic->bus_shift;
40 void __iomem *addr = asic->mapping + (REG_ADDR << bus_shift);
41 void __iomem *data = asic->mapping + (REG_DATA << bus_shift);
42
43 __raw_writeb(~READ_MODE & reg, addr);
44 __raw_writeb(val, data);
45}
46EXPORT_SYMBOL(pasic3_write_register); /* for leds-pasic3 */
47
48/*
49 * read from a secondary register on the PASIC3
50 */
51u8 pasic3_read_register(struct device *dev, u32 reg)
52{
53 struct pasic3_data *asic = dev->driver_data;
54 int bus_shift = asic->bus_shift;
55 void __iomem *addr = asic->mapping + (REG_ADDR << bus_shift);
56 void __iomem *data = asic->mapping + (REG_DATA << bus_shift);
57
58 __raw_writeb(READ_MODE | reg, addr);
59 return __raw_readb(data);
60}
61EXPORT_SYMBOL(pasic3_read_register); /* for leds-pasic3 */
62
63/*
64 * LEDs
65 */
66
Philipp Zabel0254a8f2009-02-17 10:06:45 +010067static struct mfd_cell led_cell __initdata = {
68 .name = "leds-pasic3",
69};
Philipp Zabel5dc33392008-04-12 13:25:41 +010070
71/*
72 * DS1WM
73 */
74
Philipp Zabel0254a8f2009-02-17 10:06:45 +010075static int ds1wm_enable(struct platform_device *pdev)
Philipp Zabel5dc33392008-04-12 13:25:41 +010076{
77 struct device *dev = pdev->dev.parent;
78 int c;
79
80 c = pasic3_read_register(dev, 0x28);
81 pasic3_write_register(dev, 0x28, c & 0x7f);
82
83 dev_dbg(dev, "DS1WM OWM_EN low (active) %02x\n", c & 0x7f);
Philipp Zabel0254a8f2009-02-17 10:06:45 +010084 return 0;
Philipp Zabel5dc33392008-04-12 13:25:41 +010085}
86
Philipp Zabel0254a8f2009-02-17 10:06:45 +010087static int ds1wm_disable(struct platform_device *pdev)
Philipp Zabel5dc33392008-04-12 13:25:41 +010088{
89 struct device *dev = pdev->dev.parent;
90 int c;
91
92 c = pasic3_read_register(dev, 0x28);
93 pasic3_write_register(dev, 0x28, c | 0x80);
94
95 dev_dbg(dev, "DS1WM OWM_EN high (inactive) %02x\n", c | 0x80);
Philipp Zabel0254a8f2009-02-17 10:06:45 +010096 return 0;
Philipp Zabel5dc33392008-04-12 13:25:41 +010097}
98
Philipp Zabel0254a8f2009-02-17 10:06:45 +010099static struct ds1wm_driver_data ds1wm_pdata = {
100 .active_high = 0,
Philipp Zabel5dc33392008-04-12 13:25:41 +0100101};
102
Philipp Zabel0254a8f2009-02-17 10:06:45 +0100103static struct resource ds1wm_resources[] __initdata = {
104 [0] = {
105 .start = 0,
106 .flags = IORESOURCE_MEM,
107 },
108 [1] = {
109 .start = 0,
110 .end = 0,
111 .flags = IORESOURCE_IRQ,
112 },
113};
Philipp Zabel5dc33392008-04-12 13:25:41 +0100114
Philipp Zabel0254a8f2009-02-17 10:06:45 +0100115static struct mfd_cell ds1wm_cell __initdata = {
116 .name = "ds1wm",
117 .enable = ds1wm_enable,
118 .disable = ds1wm_disable,
119 .driver_data = &ds1wm_pdata,
120 .num_resources = 2,
121 .resources = ds1wm_resources,
122};
Philipp Zabel5dc33392008-04-12 13:25:41 +0100123
124static int __init pasic3_probe(struct platform_device *pdev)
125{
126 struct pasic3_platform_data *pdata = pdev->dev.platform_data;
127 struct device *dev = &pdev->dev;
128 struct pasic3_data *asic;
129 struct resource *r;
130 int ret;
Philipp Zabel0254a8f2009-02-17 10:06:45 +0100131 int irq = 0;
132
133 r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
134 if (r) {
135 ds1wm_resources[1].flags = IORESOURCE_IRQ | (r->flags &
136 (IORESOURCE_IRQ_HIGHEDGE | IORESOURCE_IRQ_LOWEDGE));
137 irq = r->start;
138 }
139
140 r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
141 if (r) {
142 ds1wm_resources[1].flags = IORESOURCE_IRQ | (r->flags &
143 (IORESOURCE_IRQ_HIGHEDGE | IORESOURCE_IRQ_LOWEDGE));
144 irq = r->start;
145 }
Philipp Zabel5dc33392008-04-12 13:25:41 +0100146
147 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
148 if (!r)
149 return -ENXIO;
150
Philipp Zabel0254a8f2009-02-17 10:06:45 +0100151 if (!request_mem_region(r->start, resource_size(r), "pasic3"))
Philipp Zabel5dc33392008-04-12 13:25:41 +0100152 return -EBUSY;
153
154 asic = kzalloc(sizeof(struct pasic3_data), GFP_KERNEL);
155 if (!asic)
156 return -ENOMEM;
157
158 platform_set_drvdata(pdev, asic);
159
Philipp Zabel0254a8f2009-02-17 10:06:45 +0100160 asic->mapping = ioremap(r->start, resource_size(r));
Philipp Zabel5dc33392008-04-12 13:25:41 +0100161 if (!asic->mapping) {
162 dev_err(dev, "couldn't ioremap PASIC3\n");
163 kfree(asic);
164 return -ENOMEM;
165 }
166
Philipp Zabel0254a8f2009-02-17 10:06:45 +0100167 /* calculate bus shift from mem resource */
168 asic->bus_shift = (resource_size(r) - 5) >> 3;
169
Philipp Zabel47c10ed2009-02-17 10:09:44 +0100170 if (pdata && pdata->clock_rate) {
171 ds1wm_pdata.clock_rate = pdata->clock_rate;
172 /* the first 5 PASIC3 registers control the DS1WM */
173 ds1wm_resources[0].end = (5 << asic->bus_shift) - 1;
174 ds1wm_cell.platform_data = &ds1wm_cell;
175 ds1wm_cell.data_size = sizeof(ds1wm_cell);
176 ret = mfd_add_devices(&pdev->dev, pdev->id,
177 &ds1wm_cell, 1, r, irq);
178 if (ret < 0)
179 dev_warn(dev, "failed to register DS1WM\n");
180 }
Philipp Zabel5dc33392008-04-12 13:25:41 +0100181
Philipp Zabel47c10ed2009-02-17 10:09:44 +0100182 if (pdata && pdata->led_pdata) {
Philipp Zabel0254a8f2009-02-17 10:06:45 +0100183 led_cell.driver_data = pdata->led_pdata;
184 led_cell.platform_data = &led_cell;
185 led_cell.data_size = sizeof(ds1wm_cell);
186 ret = mfd_add_devices(&pdev->dev, pdev->id, &led_cell, 1, r, 0);
Philipp Zabel5dc33392008-04-12 13:25:41 +0100187 if (ret < 0)
188 dev_warn(dev, "failed to register LED device\n");
189 }
190
191 return 0;
192}
193
194static int pasic3_remove(struct platform_device *pdev)
195{
196 struct pasic3_data *asic = platform_get_drvdata(pdev);
197 struct resource *r;
198
Philipp Zabel0254a8f2009-02-17 10:06:45 +0100199 mfd_remove_devices(&pdev->dev);
Philipp Zabel5dc33392008-04-12 13:25:41 +0100200
201 iounmap(asic->mapping);
202 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel0254a8f2009-02-17 10:06:45 +0100203 release_mem_region(r->start, resource_size(r));
Philipp Zabel5dc33392008-04-12 13:25:41 +0100204 kfree(asic);
205 return 0;
206}
207
Kay Sievers4f46d6e2008-07-25 01:45:47 -0700208MODULE_ALIAS("platform:pasic3");
209
Philipp Zabel5dc33392008-04-12 13:25:41 +0100210static struct platform_driver pasic3_driver = {
211 .driver = {
212 .name = "pasic3",
213 },
214 .remove = pasic3_remove,
215};
216
217static int __init pasic3_base_init(void)
218{
219 return platform_driver_probe(&pasic3_driver, pasic3_probe);
220}
221
222static void __exit pasic3_base_exit(void)
223{
224 platform_driver_unregister(&pasic3_driver);
225}
226
227module_init(pasic3_base_init);
228module_exit(pasic3_base_exit);
229
230MODULE_AUTHOR("Philipp Zabel <philipp.zabel@gmail.com>");
231MODULE_DESCRIPTION("Core driver for HTC PASIC3");
232MODULE_LICENSE("GPL");