xref: /openbmc/linux/drivers/mfd/cs5535-mfd.c (revision fa1df691)
1f71e1afdSAndres Salomon /*
2f71e1afdSAndres Salomon  * cs5535-mfd.c - core MFD driver for CS5535/CS5536 southbridges
3f71e1afdSAndres Salomon  *
4f71e1afdSAndres Salomon  * The CS5535 and CS5536 has an ISA bridge on the PCI bus that is
5f71e1afdSAndres Salomon  * used for accessing GPIOs, MFGPTs, ACPI, etc.  Each subdevice has
6f71e1afdSAndres Salomon  * an IO range that's specified in a single BAR.  The BAR order is
7f71e1afdSAndres Salomon  * hardcoded in the CS553x specifications.
8f71e1afdSAndres Salomon  *
9f71e1afdSAndres Salomon  * Copyright (c) 2010  Andres Salomon <dilinger@queued.net>
10f71e1afdSAndres Salomon  *
11f71e1afdSAndres Salomon  * This program is free software; you can redistribute it and/or modify
12f71e1afdSAndres Salomon  * it under the terms of the GNU General Public License version 2 as
13f71e1afdSAndres Salomon  * published by the Free Software Foundation.
14f71e1afdSAndres Salomon  *
15f71e1afdSAndres Salomon  * This program is distributed in the hope that it will be useful,
16f71e1afdSAndres Salomon  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17f71e1afdSAndres Salomon  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18f71e1afdSAndres Salomon  * GNU General Public License for more details.
19f71e1afdSAndres Salomon  *
20f71e1afdSAndres Salomon  * You should have received a copy of the GNU General Public License
21f71e1afdSAndres Salomon  * along with this program; if not, write to the Free Software
22f71e1afdSAndres Salomon  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23f71e1afdSAndres Salomon  */
24f71e1afdSAndres Salomon 
25f71e1afdSAndres Salomon #include <linux/kernel.h>
26f71e1afdSAndres Salomon #include <linux/init.h>
27f71e1afdSAndres Salomon #include <linux/mfd/core.h>
28f71e1afdSAndres Salomon #include <linux/module.h>
29f71e1afdSAndres Salomon #include <linux/pci.h>
30fa1df691SAndres Salomon #include <asm/olpc.h>
31f71e1afdSAndres Salomon 
32f71e1afdSAndres Salomon #define DRV_NAME "cs5535-mfd"
33f71e1afdSAndres Salomon 
34f71e1afdSAndres Salomon enum cs5535_mfd_bars {
35f71e1afdSAndres Salomon 	SMB_BAR = 0,
36f71e1afdSAndres Salomon 	GPIO_BAR = 1,
37f71e1afdSAndres Salomon 	MFGPT_BAR = 2,
38f71e1afdSAndres Salomon 	PMS_BAR = 4,
39f71e1afdSAndres Salomon 	ACPI_BAR = 5,
40f71e1afdSAndres Salomon 	NR_BARS,
41f71e1afdSAndres Salomon };
42f71e1afdSAndres Salomon 
431310e6d6SAndres Salomon static int cs5535_mfd_res_enable(struct platform_device *pdev)
441310e6d6SAndres Salomon {
451310e6d6SAndres Salomon 	struct resource *res;
461310e6d6SAndres Salomon 
471310e6d6SAndres Salomon 	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
481310e6d6SAndres Salomon 	if (!res) {
491310e6d6SAndres Salomon 		dev_err(&pdev->dev, "can't fetch device resource info\n");
501310e6d6SAndres Salomon 		return -EIO;
511310e6d6SAndres Salomon 	}
521310e6d6SAndres Salomon 
531310e6d6SAndres Salomon 	if (!request_region(res->start, resource_size(res), DRV_NAME)) {
541310e6d6SAndres Salomon 		dev_err(&pdev->dev, "can't request region\n");
551310e6d6SAndres Salomon 		return -EIO;
561310e6d6SAndres Salomon 	}
571310e6d6SAndres Salomon 
581310e6d6SAndres Salomon 	return 0;
591310e6d6SAndres Salomon }
601310e6d6SAndres Salomon 
611310e6d6SAndres Salomon static int cs5535_mfd_res_disable(struct platform_device *pdev)
621310e6d6SAndres Salomon {
631310e6d6SAndres Salomon 	struct resource *res;
641310e6d6SAndres Salomon 	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
651310e6d6SAndres Salomon 	if (!res) {
661310e6d6SAndres Salomon 		dev_err(&pdev->dev, "can't fetch device resource info\n");
671310e6d6SAndres Salomon 		return -EIO;
681310e6d6SAndres Salomon 	}
691310e6d6SAndres Salomon 
701310e6d6SAndres Salomon 	release_region(res->start, resource_size(res));
711310e6d6SAndres Salomon 	return 0;
721310e6d6SAndres Salomon }
731310e6d6SAndres Salomon 
74f71e1afdSAndres Salomon static __devinitdata struct resource cs5535_mfd_resources[NR_BARS];
75f71e1afdSAndres Salomon 
76f71e1afdSAndres Salomon static __devinitdata struct mfd_cell cs5535_mfd_cells[] = {
77f71e1afdSAndres Salomon 	{
78f71e1afdSAndres Salomon 		.id = SMB_BAR,
79f71e1afdSAndres Salomon 		.name = "cs5535-smb",
80f71e1afdSAndres Salomon 		.num_resources = 1,
81f71e1afdSAndres Salomon 		.resources = &cs5535_mfd_resources[SMB_BAR],
82f71e1afdSAndres Salomon 	},
83f71e1afdSAndres Salomon 	{
84f71e1afdSAndres Salomon 		.id = GPIO_BAR,
85f71e1afdSAndres Salomon 		.name = "cs5535-gpio",
86f71e1afdSAndres Salomon 		.num_resources = 1,
87f71e1afdSAndres Salomon 		.resources = &cs5535_mfd_resources[GPIO_BAR],
88f71e1afdSAndres Salomon 	},
89f71e1afdSAndres Salomon 	{
90f71e1afdSAndres Salomon 		.id = MFGPT_BAR,
91f71e1afdSAndres Salomon 		.name = "cs5535-mfgpt",
92f71e1afdSAndres Salomon 		.num_resources = 1,
93f71e1afdSAndres Salomon 		.resources = &cs5535_mfd_resources[MFGPT_BAR],
94f71e1afdSAndres Salomon 	},
95f71e1afdSAndres Salomon 	{
96f71e1afdSAndres Salomon 		.id = PMS_BAR,
97f71e1afdSAndres Salomon 		.name = "cs5535-pms",
98f71e1afdSAndres Salomon 		.num_resources = 1,
99f71e1afdSAndres Salomon 		.resources = &cs5535_mfd_resources[PMS_BAR],
1001310e6d6SAndres Salomon 
1011310e6d6SAndres Salomon 		.enable = cs5535_mfd_res_enable,
1021310e6d6SAndres Salomon 		.disable = cs5535_mfd_res_disable,
103f71e1afdSAndres Salomon 	},
104f71e1afdSAndres Salomon 	{
105f71e1afdSAndres Salomon 		.id = ACPI_BAR,
106f71e1afdSAndres Salomon 		.name = "cs5535-acpi",
107f71e1afdSAndres Salomon 		.num_resources = 1,
108f71e1afdSAndres Salomon 		.resources = &cs5535_mfd_resources[ACPI_BAR],
1091310e6d6SAndres Salomon 
1101310e6d6SAndres Salomon 		.enable = cs5535_mfd_res_enable,
1111310e6d6SAndres Salomon 		.disable = cs5535_mfd_res_disable,
112f71e1afdSAndres Salomon 	},
113f71e1afdSAndres Salomon };
114f71e1afdSAndres Salomon 
115fa1df691SAndres Salomon #ifdef CONFIG_OLPC
116fa1df691SAndres Salomon static void __devinit cs5535_clone_olpc_cells(void)
117fa1df691SAndres Salomon {
118fa1df691SAndres Salomon 	const char *acpi_clones[] = { "olpc-xo1-acpi" };
119fa1df691SAndres Salomon 	const char *pms_clones[] = { "olpc-xo1-pms" };
120fa1df691SAndres Salomon 
121fa1df691SAndres Salomon 	if (!machine_is_olpc())
122fa1df691SAndres Salomon 		return;
123fa1df691SAndres Salomon 
124fa1df691SAndres Salomon 	mfd_clone_cell("cs5535-acpi", acpi_clones, ARRAY_SIZE(acpi_clones));
125fa1df691SAndres Salomon 	mfd_clone_cell("cs5535-pms", pms_clones, ARRAY_SIZE(pms_clones));
126fa1df691SAndres Salomon }
127fa1df691SAndres Salomon #else
128fa1df691SAndres Salomon static void cs5535_clone_olpc_cells(void) { }
129fa1df691SAndres Salomon #endif
130fa1df691SAndres Salomon 
131f71e1afdSAndres Salomon static int __devinit cs5535_mfd_probe(struct pci_dev *pdev,
132f71e1afdSAndres Salomon 		const struct pci_device_id *id)
133f71e1afdSAndres Salomon {
134f71e1afdSAndres Salomon 	int err, i;
135f71e1afdSAndres Salomon 
136f71e1afdSAndres Salomon 	err = pci_enable_device(pdev);
137f71e1afdSAndres Salomon 	if (err)
138f71e1afdSAndres Salomon 		return err;
139f71e1afdSAndres Salomon 
140f71e1afdSAndres Salomon 	/* fill in IO range for each cell; subdrivers handle the region */
141f71e1afdSAndres Salomon 	for (i = 0; i < ARRAY_SIZE(cs5535_mfd_cells); i++) {
142f71e1afdSAndres Salomon 		int bar = cs5535_mfd_cells[i].id;
143f71e1afdSAndres Salomon 		struct resource *r = &cs5535_mfd_resources[bar];
144f71e1afdSAndres Salomon 
145f71e1afdSAndres Salomon 		r->flags = IORESOURCE_IO;
146f71e1afdSAndres Salomon 		r->start = pci_resource_start(pdev, bar);
147f71e1afdSAndres Salomon 		r->end = pci_resource_end(pdev, bar);
148f71e1afdSAndres Salomon 
149f71e1afdSAndres Salomon 		/* id is used for temporarily storing BAR; unset it now */
150f71e1afdSAndres Salomon 		cs5535_mfd_cells[i].id = 0;
151f71e1afdSAndres Salomon 	}
152f71e1afdSAndres Salomon 
153f71e1afdSAndres Salomon 	err = mfd_add_devices(&pdev->dev, -1, cs5535_mfd_cells,
154f71e1afdSAndres Salomon 			ARRAY_SIZE(cs5535_mfd_cells), NULL, 0);
155f71e1afdSAndres Salomon 	if (err) {
156f71e1afdSAndres Salomon 		dev_err(&pdev->dev, "MFD add devices failed: %d\n", err);
157f71e1afdSAndres Salomon 		goto err_disable;
158f71e1afdSAndres Salomon 	}
159fa1df691SAndres Salomon 	cs5535_clone_olpc_cells();
160f71e1afdSAndres Salomon 
161816b4580SAndres Salomon 	dev_info(&pdev->dev, "%zu devices registered.\n",
162f71e1afdSAndres Salomon 			ARRAY_SIZE(cs5535_mfd_cells));
163f71e1afdSAndres Salomon 
164f71e1afdSAndres Salomon 	return 0;
165f71e1afdSAndres Salomon 
166f71e1afdSAndres Salomon err_disable:
167f71e1afdSAndres Salomon 	pci_disable_device(pdev);
168f71e1afdSAndres Salomon 	return err;
169f71e1afdSAndres Salomon }
170f71e1afdSAndres Salomon 
171f71e1afdSAndres Salomon static void __devexit cs5535_mfd_remove(struct pci_dev *pdev)
172f71e1afdSAndres Salomon {
173f71e1afdSAndres Salomon 	mfd_remove_devices(&pdev->dev);
174f71e1afdSAndres Salomon 	pci_disable_device(pdev);
175f71e1afdSAndres Salomon }
176f71e1afdSAndres Salomon 
177f71e1afdSAndres Salomon static struct pci_device_id cs5535_mfd_pci_tbl[] = {
178f71e1afdSAndres Salomon 	{ PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_CS5535_ISA) },
179f71e1afdSAndres Salomon 	{ PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_CS5536_ISA) },
180f71e1afdSAndres Salomon 	{ 0, }
181f71e1afdSAndres Salomon };
182f71e1afdSAndres Salomon MODULE_DEVICE_TABLE(pci, cs5535_mfd_pci_tbl);
183f71e1afdSAndres Salomon 
184f71e1afdSAndres Salomon static struct pci_driver cs5535_mfd_drv = {
185f71e1afdSAndres Salomon 	.name = DRV_NAME,
186f71e1afdSAndres Salomon 	.id_table = cs5535_mfd_pci_tbl,
187f71e1afdSAndres Salomon 	.probe = cs5535_mfd_probe,
188f71e1afdSAndres Salomon 	.remove = __devexit_p(cs5535_mfd_remove),
189f71e1afdSAndres Salomon };
190f71e1afdSAndres Salomon 
191f71e1afdSAndres Salomon static int __init cs5535_mfd_init(void)
192f71e1afdSAndres Salomon {
193f71e1afdSAndres Salomon 	return pci_register_driver(&cs5535_mfd_drv);
194f71e1afdSAndres Salomon }
195f71e1afdSAndres Salomon 
196f71e1afdSAndres Salomon static void __exit cs5535_mfd_exit(void)
197f71e1afdSAndres Salomon {
198f71e1afdSAndres Salomon 	pci_unregister_driver(&cs5535_mfd_drv);
199f71e1afdSAndres Salomon }
200f71e1afdSAndres Salomon 
201f71e1afdSAndres Salomon module_init(cs5535_mfd_init);
202f71e1afdSAndres Salomon module_exit(cs5535_mfd_exit);
203f71e1afdSAndres Salomon 
204f71e1afdSAndres Salomon MODULE_AUTHOR("Andres Salomon <dilinger@queued.net>");
205f71e1afdSAndres Salomon MODULE_DESCRIPTION("MFD driver for CS5535/CS5536 southbridge's ISA PCI device");
206f71e1afdSAndres Salomon MODULE_LICENSE("GPL");
207