Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 1 | /* |
| 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 Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 15 | #include <linux/gpio.h> |
| 16 | #include <linux/io.h> |
| 17 | #include <linux/irq.h> |
| 18 | #include <linux/interrupt.h> |
Philipp Zabel | 0254a8f | 2009-02-17 10:06:45 +0100 | [diff] [blame] | 19 | #include <linux/mfd/core.h> |
| 20 | #include <linux/mfd/ds1wm.h> |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 21 | #include <linux/mfd/htc-pasic3.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 23 | |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 24 | struct pasic3_data { |
| 25 | void __iomem *mapping; |
| 26 | unsigned int bus_shift; |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | #define REG_ADDR 5 |
| 30 | #define REG_DATA 6 |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 31 | |
| 32 | #define READ_MODE 0x80 |
| 33 | |
| 34 | /* |
| 35 | * write to a secondary register on the PASIC3 |
| 36 | */ |
| 37 | void pasic3_write_register(struct device *dev, u32 reg, u8 val) |
| 38 | { |
Greg Kroah-Hartman | 1902a9e | 2009-04-30 14:43:31 -0700 | [diff] [blame] | 39 | struct pasic3_data *asic = dev_get_drvdata(dev); |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 40 | 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 | } |
| 47 | EXPORT_SYMBOL(pasic3_write_register); /* for leds-pasic3 */ |
| 48 | |
| 49 | /* |
| 50 | * read from a secondary register on the PASIC3 |
| 51 | */ |
| 52 | u8 pasic3_read_register(struct device *dev, u32 reg) |
| 53 | { |
Greg Kroah-Hartman | 1902a9e | 2009-04-30 14:43:31 -0700 | [diff] [blame] | 54 | struct pasic3_data *asic = dev_get_drvdata(dev); |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 55 | 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 | } |
| 62 | EXPORT_SYMBOL(pasic3_read_register); /* for leds-pasic3 */ |
| 63 | |
| 64 | /* |
| 65 | * LEDs |
| 66 | */ |
| 67 | |
Philipp Zabel | 0254a8f | 2009-02-17 10:06:45 +0100 | [diff] [blame] | 68 | static struct mfd_cell led_cell __initdata = { |
| 69 | .name = "leds-pasic3", |
| 70 | }; |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 71 | |
| 72 | /* |
| 73 | * DS1WM |
| 74 | */ |
| 75 | |
Philipp Zabel | 0254a8f | 2009-02-17 10:06:45 +0100 | [diff] [blame] | 76 | static int ds1wm_enable(struct platform_device *pdev) |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 77 | { |
| 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 Zabel | 0254a8f | 2009-02-17 10:06:45 +0100 | [diff] [blame] | 85 | return 0; |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 86 | } |
| 87 | |
Philipp Zabel | 0254a8f | 2009-02-17 10:06:45 +0100 | [diff] [blame] | 88 | static int ds1wm_disable(struct platform_device *pdev) |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 89 | { |
| 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 Zabel | 0254a8f | 2009-02-17 10:06:45 +0100 | [diff] [blame] | 97 | return 0; |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 98 | } |
| 99 | |
Philipp Zabel | 0254a8f | 2009-02-17 10:06:45 +0100 | [diff] [blame] | 100 | static struct ds1wm_driver_data ds1wm_pdata = { |
| 101 | .active_high = 0, |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 102 | }; |
| 103 | |
Philipp Zabel | 0254a8f | 2009-02-17 10:06:45 +0100 | [diff] [blame] | 104 | static struct resource ds1wm_resources[] __initdata = { |
| 105 | [0] = { |
| 106 | .start = 0, |
| 107 | .flags = IORESOURCE_MEM, |
| 108 | }, |
| 109 | [1] = { |
| 110 | .start = 0, |
| 111 | .end = 0, |
| 112 | .flags = IORESOURCE_IRQ, |
| 113 | }, |
| 114 | }; |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 115 | |
Philipp Zabel | 0254a8f | 2009-02-17 10:06:45 +0100 | [diff] [blame] | 116 | static struct mfd_cell ds1wm_cell __initdata = { |
| 117 | .name = "ds1wm", |
| 118 | .enable = ds1wm_enable, |
| 119 | .disable = ds1wm_disable, |
| 120 | .driver_data = &ds1wm_pdata, |
| 121 | .num_resources = 2, |
| 122 | .resources = ds1wm_resources, |
| 123 | }; |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 124 | |
| 125 | static int __init pasic3_probe(struct platform_device *pdev) |
| 126 | { |
| 127 | struct pasic3_platform_data *pdata = pdev->dev.platform_data; |
| 128 | struct device *dev = &pdev->dev; |
| 129 | struct pasic3_data *asic; |
| 130 | struct resource *r; |
| 131 | int ret; |
Philipp Zabel | 0254a8f | 2009-02-17 10:06:45 +0100 | [diff] [blame] | 132 | int irq = 0; |
| 133 | |
| 134 | r = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
| 135 | if (r) { |
| 136 | ds1wm_resources[1].flags = IORESOURCE_IRQ | (r->flags & |
| 137 | (IORESOURCE_IRQ_HIGHEDGE | IORESOURCE_IRQ_LOWEDGE)); |
| 138 | irq = r->start; |
| 139 | } |
| 140 | |
| 141 | r = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
| 142 | if (r) { |
| 143 | ds1wm_resources[1].flags = IORESOURCE_IRQ | (r->flags & |
| 144 | (IORESOURCE_IRQ_HIGHEDGE | IORESOURCE_IRQ_LOWEDGE)); |
| 145 | irq = r->start; |
| 146 | } |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 147 | |
| 148 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 149 | if (!r) |
| 150 | return -ENXIO; |
| 151 | |
Philipp Zabel | 0254a8f | 2009-02-17 10:06:45 +0100 | [diff] [blame] | 152 | if (!request_mem_region(r->start, resource_size(r), "pasic3")) |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 153 | return -EBUSY; |
| 154 | |
| 155 | asic = kzalloc(sizeof(struct pasic3_data), GFP_KERNEL); |
| 156 | if (!asic) |
| 157 | return -ENOMEM; |
| 158 | |
| 159 | platform_set_drvdata(pdev, asic); |
| 160 | |
Philipp Zabel | 0254a8f | 2009-02-17 10:06:45 +0100 | [diff] [blame] | 161 | asic->mapping = ioremap(r->start, resource_size(r)); |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 162 | if (!asic->mapping) { |
| 163 | dev_err(dev, "couldn't ioremap PASIC3\n"); |
| 164 | kfree(asic); |
| 165 | return -ENOMEM; |
| 166 | } |
| 167 | |
Philipp Zabel | 0254a8f | 2009-02-17 10:06:45 +0100 | [diff] [blame] | 168 | /* calculate bus shift from mem resource */ |
| 169 | asic->bus_shift = (resource_size(r) - 5) >> 3; |
| 170 | |
Philipp Zabel | 47c10ed | 2009-02-17 10:09:44 +0100 | [diff] [blame] | 171 | if (pdata && pdata->clock_rate) { |
| 172 | ds1wm_pdata.clock_rate = pdata->clock_rate; |
| 173 | /* the first 5 PASIC3 registers control the DS1WM */ |
| 174 | ds1wm_resources[0].end = (5 << asic->bus_shift) - 1; |
| 175 | ds1wm_cell.platform_data = &ds1wm_cell; |
| 176 | ds1wm_cell.data_size = sizeof(ds1wm_cell); |
| 177 | ret = mfd_add_devices(&pdev->dev, pdev->id, |
| 178 | &ds1wm_cell, 1, r, irq); |
| 179 | if (ret < 0) |
| 180 | dev_warn(dev, "failed to register DS1WM\n"); |
| 181 | } |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 182 | |
Philipp Zabel | 47c10ed | 2009-02-17 10:09:44 +0100 | [diff] [blame] | 183 | if (pdata && pdata->led_pdata) { |
Philipp Zabel | 0254a8f | 2009-02-17 10:06:45 +0100 | [diff] [blame] | 184 | led_cell.driver_data = pdata->led_pdata; |
| 185 | led_cell.platform_data = &led_cell; |
| 186 | led_cell.data_size = sizeof(ds1wm_cell); |
| 187 | ret = mfd_add_devices(&pdev->dev, pdev->id, &led_cell, 1, r, 0); |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 188 | if (ret < 0) |
| 189 | dev_warn(dev, "failed to register LED device\n"); |
| 190 | } |
| 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | static int pasic3_remove(struct platform_device *pdev) |
| 196 | { |
| 197 | struct pasic3_data *asic = platform_get_drvdata(pdev); |
| 198 | struct resource *r; |
| 199 | |
Philipp Zabel | 0254a8f | 2009-02-17 10:06:45 +0100 | [diff] [blame] | 200 | mfd_remove_devices(&pdev->dev); |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 201 | |
| 202 | iounmap(asic->mapping); |
| 203 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Philipp Zabel | 0254a8f | 2009-02-17 10:06:45 +0100 | [diff] [blame] | 204 | release_mem_region(r->start, resource_size(r)); |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 205 | kfree(asic); |
| 206 | return 0; |
| 207 | } |
| 208 | |
Kay Sievers | 4f46d6e | 2008-07-25 01:45:47 -0700 | [diff] [blame] | 209 | MODULE_ALIAS("platform:pasic3"); |
| 210 | |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 211 | static struct platform_driver pasic3_driver = { |
| 212 | .driver = { |
| 213 | .name = "pasic3", |
| 214 | }, |
| 215 | .remove = pasic3_remove, |
| 216 | }; |
| 217 | |
| 218 | static int __init pasic3_base_init(void) |
| 219 | { |
| 220 | return platform_driver_probe(&pasic3_driver, pasic3_probe); |
| 221 | } |
| 222 | |
| 223 | static void __exit pasic3_base_exit(void) |
| 224 | { |
| 225 | platform_driver_unregister(&pasic3_driver); |
| 226 | } |
| 227 | |
| 228 | module_init(pasic3_base_init); |
| 229 | module_exit(pasic3_base_exit); |
| 230 | |
| 231 | MODULE_AUTHOR("Philipp Zabel <philipp.zabel@gmail.com>"); |
| 232 | MODULE_DESCRIPTION("Core driver for HTC PASIC3"); |
| 233 | MODULE_LICENSE("GPL"); |