Thomas Gleixner | b886d83c | 2019-06-01 10:08:55 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Core driver for HTC PASIC3 LED/DS1WM chip. |
| 4 | * |
| 5 | * Copyright (C) 2006 Philipp Zabel <philipp.zabel@gmail.com> |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/init.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/platform_device.h> |
| 11 | |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 12 | #include <linux/gpio.h> |
| 13 | #include <linux/io.h> |
| 14 | #include <linux/irq.h> |
| 15 | #include <linux/interrupt.h> |
Philipp Zabel | 0254a8f | 2009-02-17 10:06:45 +0100 | [diff] [blame] | 16 | #include <linux/mfd/core.h> |
| 17 | #include <linux/mfd/ds1wm.h> |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 18 | #include <linux/mfd/htc-pasic3.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 20 | |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 21 | struct pasic3_data { |
| 22 | void __iomem *mapping; |
| 23 | unsigned int bus_shift; |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 24 | }; |
| 25 | |
| 26 | #define REG_ADDR 5 |
| 27 | #define REG_DATA 6 |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 28 | |
| 29 | #define READ_MODE 0x80 |
| 30 | |
| 31 | /* |
| 32 | * write to a secondary register on the PASIC3 |
| 33 | */ |
| 34 | void pasic3_write_register(struct device *dev, u32 reg, u8 val) |
| 35 | { |
Greg Kroah-Hartman | 1902a9e | 2009-04-30 14:43:31 -0700 | [diff] [blame] | 36 | struct pasic3_data *asic = dev_get_drvdata(dev); |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 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); |
| 40 | |
| 41 | __raw_writeb(~READ_MODE & reg, addr); |
| 42 | __raw_writeb(val, data); |
| 43 | } |
| 44 | EXPORT_SYMBOL(pasic3_write_register); /* for leds-pasic3 */ |
| 45 | |
| 46 | /* |
| 47 | * read from a secondary register on the PASIC3 |
| 48 | */ |
| 49 | u8 pasic3_read_register(struct device *dev, u32 reg) |
| 50 | { |
Greg Kroah-Hartman | 1902a9e | 2009-04-30 14:43:31 -0700 | [diff] [blame] | 51 | struct pasic3_data *asic = dev_get_drvdata(dev); |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 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); |
| 55 | |
| 56 | __raw_writeb(READ_MODE | reg, addr); |
| 57 | return __raw_readb(data); |
| 58 | } |
| 59 | EXPORT_SYMBOL(pasic3_read_register); /* for leds-pasic3 */ |
| 60 | |
| 61 | /* |
| 62 | * LEDs |
| 63 | */ |
| 64 | |
Philipp Zabel | 0254a8f | 2009-02-17 10:06:45 +0100 | [diff] [blame] | 65 | static struct mfd_cell led_cell __initdata = { |
| 66 | .name = "leds-pasic3", |
| 67 | }; |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 68 | |
| 69 | /* |
| 70 | * DS1WM |
| 71 | */ |
| 72 | |
Philipp Zabel | 0254a8f | 2009-02-17 10:06:45 +0100 | [diff] [blame] | 73 | static int ds1wm_enable(struct platform_device *pdev) |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 74 | { |
| 75 | struct device *dev = pdev->dev.parent; |
| 76 | int c; |
| 77 | |
| 78 | c = pasic3_read_register(dev, 0x28); |
| 79 | pasic3_write_register(dev, 0x28, c & 0x7f); |
| 80 | |
| 81 | dev_dbg(dev, "DS1WM OWM_EN low (active) %02x\n", c & 0x7f); |
Philipp Zabel | 0254a8f | 2009-02-17 10:06:45 +0100 | [diff] [blame] | 82 | return 0; |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 83 | } |
| 84 | |
Philipp Zabel | 0254a8f | 2009-02-17 10:06:45 +0100 | [diff] [blame] | 85 | static int ds1wm_disable(struct platform_device *pdev) |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 86 | { |
| 87 | struct device *dev = pdev->dev.parent; |
| 88 | int c; |
| 89 | |
| 90 | c = pasic3_read_register(dev, 0x28); |
| 91 | pasic3_write_register(dev, 0x28, c | 0x80); |
| 92 | |
| 93 | dev_dbg(dev, "DS1WM OWM_EN high (inactive) %02x\n", c | 0x80); |
Philipp Zabel | 0254a8f | 2009-02-17 10:06:45 +0100 | [diff] [blame] | 94 | return 0; |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 95 | } |
| 96 | |
Philipp Zabel | 0254a8f | 2009-02-17 10:06:45 +0100 | [diff] [blame] | 97 | static struct ds1wm_driver_data ds1wm_pdata = { |
| 98 | .active_high = 0, |
Jean-François Dagenais | f607e7f | 2011-07-08 15:39:44 -0700 | [diff] [blame] | 99 | .reset_recover_delay = 1, |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 100 | }; |
| 101 | |
Philipp Zabel | 0254a8f | 2009-02-17 10:06:45 +0100 | [diff] [blame] | 102 | static struct resource ds1wm_resources[] __initdata = { |
| 103 | [0] = { |
| 104 | .start = 0, |
| 105 | .flags = IORESOURCE_MEM, |
| 106 | }, |
| 107 | [1] = { |
| 108 | .start = 0, |
| 109 | .end = 0, |
| 110 | .flags = IORESOURCE_IRQ, |
| 111 | }, |
| 112 | }; |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 113 | |
Geert Uytterhoeven | 5ac9855 | 2013-11-18 14:33:06 +0100 | [diff] [blame] | 114 | static const struct mfd_cell ds1wm_cell __initconst = { |
Philipp Zabel | 0254a8f | 2009-02-17 10:06:45 +0100 | [diff] [blame] | 115 | .name = "ds1wm", |
| 116 | .enable = ds1wm_enable, |
| 117 | .disable = ds1wm_disable, |
Samuel Ortiz | 121ea57 | 2011-04-06 11:41:03 +0200 | [diff] [blame] | 118 | .platform_data = &ds1wm_pdata, |
| 119 | .pdata_size = sizeof(ds1wm_pdata), |
Philipp Zabel | 0254a8f | 2009-02-17 10:06:45 +0100 | [diff] [blame] | 120 | .num_resources = 2, |
| 121 | .resources = ds1wm_resources, |
| 122 | }; |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 123 | |
| 124 | static int __init pasic3_probe(struct platform_device *pdev) |
| 125 | { |
Jingoo Han | 334a41ce | 2013-07-30 17:10:05 +0900 | [diff] [blame] | 126 | struct pasic3_platform_data *pdata = dev_get_platdata(&pdev->dev); |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 127 | struct device *dev = &pdev->dev; |
| 128 | struct pasic3_data *asic; |
| 129 | struct resource *r; |
| 130 | int ret; |
Philipp Zabel | 0254a8f | 2009-02-17 10:06:45 +0100 | [diff] [blame] | 131 | 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 | |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 140 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 141 | if (!r) |
| 142 | return -ENXIO; |
| 143 | |
Philipp Zabel | 0254a8f | 2009-02-17 10:06:45 +0100 | [diff] [blame] | 144 | if (!request_mem_region(r->start, resource_size(r), "pasic3")) |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 145 | return -EBUSY; |
| 146 | |
Lee Jones | 8692881 | 2013-05-23 16:25:14 +0100 | [diff] [blame] | 147 | asic = devm_kzalloc(dev, sizeof(struct pasic3_data), GFP_KERNEL); |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 148 | if (!asic) |
| 149 | return -ENOMEM; |
| 150 | |
| 151 | platform_set_drvdata(pdev, asic); |
| 152 | |
Philipp Zabel | 0254a8f | 2009-02-17 10:06:45 +0100 | [diff] [blame] | 153 | asic->mapping = ioremap(r->start, resource_size(r)); |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 154 | if (!asic->mapping) { |
| 155 | dev_err(dev, "couldn't ioremap PASIC3\n"); |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 156 | return -ENOMEM; |
| 157 | } |
| 158 | |
Philipp Zabel | 0254a8f | 2009-02-17 10:06:45 +0100 | [diff] [blame] | 159 | /* calculate bus shift from mem resource */ |
| 160 | asic->bus_shift = (resource_size(r) - 5) >> 3; |
| 161 | |
Philipp Zabel | 47c10ed | 2009-02-17 10:09:44 +0100 | [diff] [blame] | 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; |
Philipp Zabel | 47c10ed | 2009-02-17 10:09:44 +0100 | [diff] [blame] | 166 | ret = mfd_add_devices(&pdev->dev, pdev->id, |
Mark Brown | 0848c94 | 2012-09-11 15:16:36 +0800 | [diff] [blame] | 167 | &ds1wm_cell, 1, r, irq, NULL); |
Philipp Zabel | 47c10ed | 2009-02-17 10:09:44 +0100 | [diff] [blame] | 168 | if (ret < 0) |
| 169 | dev_warn(dev, "failed to register DS1WM\n"); |
| 170 | } |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 171 | |
Philipp Zabel | 47c10ed | 2009-02-17 10:09:44 +0100 | [diff] [blame] | 172 | if (pdata && pdata->led_pdata) { |
Samuel Ortiz | 8ac93be | 2011-04-06 11:48:53 +0200 | [diff] [blame] | 173 | led_cell.platform_data = pdata->led_pdata; |
| 174 | led_cell.pdata_size = sizeof(struct pasic3_leds_machinfo); |
Mark Brown | 0848c94 | 2012-09-11 15:16:36 +0800 | [diff] [blame] | 175 | ret = mfd_add_devices(&pdev->dev, pdev->id, &led_cell, 1, r, |
| 176 | 0, NULL); |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 177 | if (ret < 0) |
| 178 | dev_warn(dev, "failed to register LED device\n"); |
| 179 | } |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | static int pasic3_remove(struct platform_device *pdev) |
| 185 | { |
| 186 | struct pasic3_data *asic = platform_get_drvdata(pdev); |
| 187 | struct resource *r; |
| 188 | |
Philipp Zabel | 0254a8f | 2009-02-17 10:06:45 +0100 | [diff] [blame] | 189 | mfd_remove_devices(&pdev->dev); |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 190 | |
| 191 | iounmap(asic->mapping); |
| 192 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Philipp Zabel | 0254a8f | 2009-02-17 10:06:45 +0100 | [diff] [blame] | 193 | release_mem_region(r->start, resource_size(r)); |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 194 | return 0; |
| 195 | } |
| 196 | |
Kay Sievers | 4f46d6e | 2008-07-25 01:45:47 -0700 | [diff] [blame] | 197 | MODULE_ALIAS("platform:pasic3"); |
| 198 | |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 199 | static struct platform_driver pasic3_driver = { |
| 200 | .driver = { |
| 201 | .name = "pasic3", |
| 202 | }, |
| 203 | .remove = pasic3_remove, |
| 204 | }; |
| 205 | |
Jingoo Han | e8fe6a8 | 2013-03-05 13:47:59 +0900 | [diff] [blame] | 206 | module_platform_driver_probe(pasic3_driver, pasic3_probe); |
Philipp Zabel | 5dc3339 | 2008-04-12 13:25:41 +0100 | [diff] [blame] | 207 | |
| 208 | MODULE_AUTHOR("Philipp Zabel <philipp.zabel@gmail.com>"); |
| 209 | MODULE_DESCRIPTION("Core driver for HTC PASIC3"); |
| 210 | MODULE_LICENSE("GPL"); |