blob: 6bf92a507b9546b911e258037c2dba3ac391851b [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Philipp Zabel5dc33392008-04-12 13:25:41 +010023
Philipp Zabel5dc33392008-04-12 13:25:41 +010024struct pasic3_data {
25 void __iomem *mapping;
26 unsigned int bus_shift;
Philipp Zabel5dc33392008-04-12 13:25:41 +010027};
28
29#define REG_ADDR 5
30#define REG_DATA 6
Philipp Zabel5dc33392008-04-12 13:25:41 +010031
32#define READ_MODE 0x80
33
34/*
35 * write to a secondary register on the PASIC3
36 */
37void pasic3_write_register(struct device *dev, u32 reg, u8 val)
38{
Greg Kroah-Hartman1902a9e2009-04-30 14:43:31 -070039 struct pasic3_data *asic = dev_get_drvdata(dev);
Philipp Zabel5dc33392008-04-12 13:25:41 +010040 int bus_shift = asic->bus_shift;
41 void __iomem *addr = asic->mapping + (REG_ADDR << bus_shift);
42 void __iomem *data = asic->mapping + (REG_DATA << bus_shift);
43
44 __raw_writeb(~READ_MODE & reg, addr);
45 __raw_writeb(val, data);
46}
47EXPORT_SYMBOL(pasic3_write_register); /* for leds-pasic3 */
48
49/*
50 * read from a secondary register on the PASIC3
51 */
52u8 pasic3_read_register(struct device *dev, u32 reg)
53{
Greg Kroah-Hartman1902a9e2009-04-30 14:43:31 -070054 struct pasic3_data *asic = dev_get_drvdata(dev);
Philipp Zabel5dc33392008-04-12 13:25:41 +010055 int bus_shift = asic->bus_shift;
56 void __iomem *addr = asic->mapping + (REG_ADDR << bus_shift);
57 void __iomem *data = asic->mapping + (REG_DATA << bus_shift);
58
59 __raw_writeb(READ_MODE | reg, addr);
60 return __raw_readb(data);
61}
62EXPORT_SYMBOL(pasic3_read_register); /* for leds-pasic3 */
63
64/*
65 * LEDs
66 */
67
Philipp Zabel0254a8f2009-02-17 10:06:45 +010068static struct mfd_cell led_cell __initdata = {
69 .name = "leds-pasic3",
70};
Philipp Zabel5dc33392008-04-12 13:25:41 +010071
72/*
73 * DS1WM
74 */
75
Philipp Zabel0254a8f2009-02-17 10:06:45 +010076static int ds1wm_enable(struct platform_device *pdev)
Philipp Zabel5dc33392008-04-12 13:25:41 +010077{
78 struct device *dev = pdev->dev.parent;
79 int c;
80
81 c = pasic3_read_register(dev, 0x28);
82 pasic3_write_register(dev, 0x28, c & 0x7f);
83
84 dev_dbg(dev, "DS1WM OWM_EN low (active) %02x\n", c & 0x7f);
Philipp Zabel0254a8f2009-02-17 10:06:45 +010085 return 0;
Philipp Zabel5dc33392008-04-12 13:25:41 +010086}
87
Philipp Zabel0254a8f2009-02-17 10:06:45 +010088static int ds1wm_disable(struct platform_device *pdev)
Philipp Zabel5dc33392008-04-12 13:25:41 +010089{
90 struct device *dev = pdev->dev.parent;
91 int c;
92
93 c = pasic3_read_register(dev, 0x28);
94 pasic3_write_register(dev, 0x28, c | 0x80);
95
96 dev_dbg(dev, "DS1WM OWM_EN high (inactive) %02x\n", c | 0x80);
Philipp Zabel0254a8f2009-02-17 10:06:45 +010097 return 0;
Philipp Zabel5dc33392008-04-12 13:25:41 +010098}
99
Philipp Zabel0254a8f2009-02-17 10:06:45 +0100100static struct ds1wm_driver_data ds1wm_pdata = {
101 .active_high = 0,
Jean-François Dagenaisf607e7f2011-07-08 15:39:44 -0700102 .reset_recover_delay = 1,
Philipp Zabel5dc33392008-04-12 13:25:41 +0100103};
104
Philipp Zabel0254a8f2009-02-17 10:06:45 +0100105static struct resource ds1wm_resources[] __initdata = {
106 [0] = {
107 .start = 0,
108 .flags = IORESOURCE_MEM,
109 },
110 [1] = {
111 .start = 0,
112 .end = 0,
113 .flags = IORESOURCE_IRQ,
114 },
115};
Philipp Zabel5dc33392008-04-12 13:25:41 +0100116
Philipp Zabel0254a8f2009-02-17 10:06:45 +0100117static struct mfd_cell ds1wm_cell __initdata = {
118 .name = "ds1wm",
119 .enable = ds1wm_enable,
120 .disable = ds1wm_disable,
Samuel Ortiz121ea572011-04-06 11:41:03 +0200121 .platform_data = &ds1wm_pdata,
122 .pdata_size = sizeof(ds1wm_pdata),
Philipp Zabel0254a8f2009-02-17 10:06:45 +0100123 .num_resources = 2,
124 .resources = ds1wm_resources,
125};
Philipp Zabel5dc33392008-04-12 13:25:41 +0100126
127static int __init pasic3_probe(struct platform_device *pdev)
128{
Jingoo Han334a41c2013-07-30 17:10:05 +0900129 struct pasic3_platform_data *pdata = dev_get_platdata(&pdev->dev);
Philipp Zabel5dc33392008-04-12 13:25:41 +0100130 struct device *dev = &pdev->dev;
131 struct pasic3_data *asic;
132 struct resource *r;
133 int ret;
Philipp Zabel0254a8f2009-02-17 10:06:45 +0100134 int irq = 0;
135
136 r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
137 if (r) {
138 ds1wm_resources[1].flags = IORESOURCE_IRQ | (r->flags &
139 (IORESOURCE_IRQ_HIGHEDGE | IORESOURCE_IRQ_LOWEDGE));
140 irq = r->start;
141 }
142
Philipp Zabel5dc33392008-04-12 13:25:41 +0100143 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
144 if (!r)
145 return -ENXIO;
146
Philipp Zabel0254a8f2009-02-17 10:06:45 +0100147 if (!request_mem_region(r->start, resource_size(r), "pasic3"))
Philipp Zabel5dc33392008-04-12 13:25:41 +0100148 return -EBUSY;
149
Lee Jones86928812013-05-23 16:25:14 +0100150 asic = devm_kzalloc(dev, sizeof(struct pasic3_data), GFP_KERNEL);
Philipp Zabel5dc33392008-04-12 13:25:41 +0100151 if (!asic)
152 return -ENOMEM;
153
154 platform_set_drvdata(pdev, asic);
155
Philipp Zabel0254a8f2009-02-17 10:06:45 +0100156 asic->mapping = ioremap(r->start, resource_size(r));
Philipp Zabel5dc33392008-04-12 13:25:41 +0100157 if (!asic->mapping) {
158 dev_err(dev, "couldn't ioremap PASIC3\n");
Philipp Zabel5dc33392008-04-12 13:25:41 +0100159 return -ENOMEM;
160 }
161
Philipp Zabel0254a8f2009-02-17 10:06:45 +0100162 /* calculate bus shift from mem resource */
163 asic->bus_shift = (resource_size(r) - 5) >> 3;
164
Philipp Zabel47c10ed2009-02-17 10:09:44 +0100165 if (pdata && pdata->clock_rate) {
166 ds1wm_pdata.clock_rate = pdata->clock_rate;
167 /* the first 5 PASIC3 registers control the DS1WM */
168 ds1wm_resources[0].end = (5 << asic->bus_shift) - 1;
Philipp Zabel47c10ed2009-02-17 10:09:44 +0100169 ret = mfd_add_devices(&pdev->dev, pdev->id,
Mark Brown0848c942012-09-11 15:16:36 +0800170 &ds1wm_cell, 1, r, irq, NULL);
Philipp Zabel47c10ed2009-02-17 10:09:44 +0100171 if (ret < 0)
172 dev_warn(dev, "failed to register DS1WM\n");
173 }
Philipp Zabel5dc33392008-04-12 13:25:41 +0100174
Philipp Zabel47c10ed2009-02-17 10:09:44 +0100175 if (pdata && pdata->led_pdata) {
Samuel Ortiz8ac93be2011-04-06 11:48:53 +0200176 led_cell.platform_data = pdata->led_pdata;
177 led_cell.pdata_size = sizeof(struct pasic3_leds_machinfo);
Mark Brown0848c942012-09-11 15:16:36 +0800178 ret = mfd_add_devices(&pdev->dev, pdev->id, &led_cell, 1, r,
179 0, NULL);
Philipp Zabel5dc33392008-04-12 13:25:41 +0100180 if (ret < 0)
181 dev_warn(dev, "failed to register LED device\n");
182 }
183
184 return 0;
185}
186
187static int pasic3_remove(struct platform_device *pdev)
188{
189 struct pasic3_data *asic = platform_get_drvdata(pdev);
190 struct resource *r;
191
Philipp Zabel0254a8f2009-02-17 10:06:45 +0100192 mfd_remove_devices(&pdev->dev);
Philipp Zabel5dc33392008-04-12 13:25:41 +0100193
194 iounmap(asic->mapping);
195 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel0254a8f2009-02-17 10:06:45 +0100196 release_mem_region(r->start, resource_size(r));
Philipp Zabel5dc33392008-04-12 13:25:41 +0100197 return 0;
198}
199
Kay Sievers4f46d6e2008-07-25 01:45:47 -0700200MODULE_ALIAS("platform:pasic3");
201
Philipp Zabel5dc33392008-04-12 13:25:41 +0100202static struct platform_driver pasic3_driver = {
203 .driver = {
204 .name = "pasic3",
205 },
206 .remove = pasic3_remove,
207};
208
Jingoo Hane8fe6a82013-03-05 13:47:59 +0900209module_platform_driver_probe(pasic3_driver, pasic3_probe);
Philipp Zabel5dc33392008-04-12 13:25:41 +0100210
211MODULE_AUTHOR("Philipp Zabel <philipp.zabel@gmail.com>");
212MODULE_DESCRIPTION("Core driver for HTC PASIC3");
213MODULE_LICENSE("GPL");