1 // SPDX-License-Identifier: GPL-2.0-only
3 * Core driver for HTC PASIC3 LED/DS1WM chip.
5 * Copyright (C) 2006 Philipp Zabel <philipp.zabel@gmail.com>
8 #include <linux/init.h>
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
12 #include <linux/gpio.h>
14 #include <linux/irq.h>
15 #include <linux/interrupt.h>
16 #include <linux/mfd/core.h>
17 #include <linux/mfd/ds1wm.h>
18 #include <linux/mfd/htc-pasic3.h>
19 #include <linux/slab.h>
22 void __iomem
*mapping
;
23 unsigned int bus_shift
;
29 #define READ_MODE 0x80
32 * write to a secondary register on the PASIC3
34 void pasic3_write_register(struct device
*dev
, u32 reg
, u8 val
)
36 struct pasic3_data
*asic
= dev_get_drvdata(dev
);
37 int bus_shift
= asic
->bus_shift
;
38 void __iomem
*addr
= asic
->mapping
+ (REG_ADDR
<< bus_shift
);
39 void __iomem
*data
= asic
->mapping
+ (REG_DATA
<< bus_shift
);
41 __raw_writeb(~READ_MODE
& reg
, addr
);
42 __raw_writeb(val
, data
);
44 EXPORT_SYMBOL(pasic3_write_register
); /* for leds-pasic3 */
47 * read from a secondary register on the PASIC3
49 u8
pasic3_read_register(struct device
*dev
, u32 reg
)
51 struct pasic3_data
*asic
= dev_get_drvdata(dev
);
52 int bus_shift
= asic
->bus_shift
;
53 void __iomem
*addr
= asic
->mapping
+ (REG_ADDR
<< bus_shift
);
54 void __iomem
*data
= asic
->mapping
+ (REG_DATA
<< bus_shift
);
56 __raw_writeb(READ_MODE
| reg
, addr
);
57 return __raw_readb(data
);
59 EXPORT_SYMBOL(pasic3_read_register
); /* for leds-pasic3 */
65 static struct mfd_cell led_cell __initdata
= {
66 .name
= "leds-pasic3",
73 static int ds1wm_enable(struct platform_device
*pdev
)
75 struct device
*dev
= pdev
->dev
.parent
;
78 c
= pasic3_read_register(dev
, 0x28);
79 pasic3_write_register(dev
, 0x28, c
& 0x7f);
81 dev_dbg(dev
, "DS1WM OWM_EN low (active) %02x\n", c
& 0x7f);
85 static int ds1wm_disable(struct platform_device
*pdev
)
87 struct device
*dev
= pdev
->dev
.parent
;
90 c
= pasic3_read_register(dev
, 0x28);
91 pasic3_write_register(dev
, 0x28, c
| 0x80);
93 dev_dbg(dev
, "DS1WM OWM_EN high (inactive) %02x\n", c
| 0x80);
97 static struct ds1wm_driver_data ds1wm_pdata
= {
99 .reset_recover_delay
= 1,
102 static struct resource ds1wm_resources
[] __initdata
= {
105 .flags
= IORESOURCE_MEM
,
110 .flags
= IORESOURCE_IRQ
,
114 static const struct mfd_cell ds1wm_cell __initconst
= {
116 .enable
= ds1wm_enable
,
117 .disable
= ds1wm_disable
,
118 .platform_data
= &ds1wm_pdata
,
119 .pdata_size
= sizeof(ds1wm_pdata
),
121 .resources
= ds1wm_resources
,
124 static int __init
pasic3_probe(struct platform_device
*pdev
)
126 struct pasic3_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
127 struct device
*dev
= &pdev
->dev
;
128 struct pasic3_data
*asic
;
133 r
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
135 ds1wm_resources
[1].flags
= IORESOURCE_IRQ
| (r
->flags
&
136 (IORESOURCE_IRQ_HIGHEDGE
| IORESOURCE_IRQ_LOWEDGE
));
140 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
144 if (!request_mem_region(r
->start
, resource_size(r
), "pasic3"))
147 asic
= devm_kzalloc(dev
, sizeof(struct pasic3_data
), GFP_KERNEL
);
151 platform_set_drvdata(pdev
, asic
);
153 asic
->mapping
= ioremap(r
->start
, resource_size(r
));
154 if (!asic
->mapping
) {
155 dev_err(dev
, "couldn't ioremap PASIC3\n");
159 /* calculate bus shift from mem resource */
160 asic
->bus_shift
= (resource_size(r
) - 5) >> 3;
162 if (pdata
&& pdata
->clock_rate
) {
163 ds1wm_pdata
.clock_rate
= pdata
->clock_rate
;
164 /* the first 5 PASIC3 registers control the DS1WM */
165 ds1wm_resources
[0].end
= (5 << asic
->bus_shift
) - 1;
166 ret
= mfd_add_devices(&pdev
->dev
, pdev
->id
,
167 &ds1wm_cell
, 1, r
, irq
, NULL
);
169 dev_warn(dev
, "failed to register DS1WM\n");
172 if (pdata
&& pdata
->led_pdata
) {
173 led_cell
.platform_data
= pdata
->led_pdata
;
174 led_cell
.pdata_size
= sizeof(struct pasic3_leds_machinfo
);
175 ret
= mfd_add_devices(&pdev
->dev
, pdev
->id
, &led_cell
, 1, r
,
178 dev_warn(dev
, "failed to register LED device\n");
184 static int pasic3_remove(struct platform_device
*pdev
)
186 struct pasic3_data
*asic
= platform_get_drvdata(pdev
);
189 mfd_remove_devices(&pdev
->dev
);
191 iounmap(asic
->mapping
);
192 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
193 release_mem_region(r
->start
, resource_size(r
));
197 MODULE_ALIAS("platform:pasic3");
199 static struct platform_driver pasic3_driver
= {
203 .remove
= pasic3_remove
,
206 module_platform_driver_probe(pasic3_driver
, pasic3_probe
);
208 MODULE_AUTHOR("Philipp Zabel <philipp.zabel@gmail.com>");
209 MODULE_DESCRIPTION("Core driver for HTC PASIC3");
210 MODULE_LICENSE("GPL");