aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/mfd/cs5535-mfd.c
blob: d0fb2e52ee76a82ab18f668f41b10ac3f4e01ed3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// SPDX-License-Identifier: GPL-2.0-only
/*
 * cs5535-mfd.c - core MFD driver for CS5535/CS5536 southbridges
 *
 * The CS5535 and CS5536 has an ISA bridge on the PCI bus that is
 * used for accessing GPIOs, MFGPTs, ACPI, etc.  Each subdevice has
 * an IO range that's specified in a single BAR.  The BAR order is
 * hardcoded in the CS553x specifications.
 *
 * Copyright (c) 2010  Andres Salomon <dilinger@queued.net>
 */

#include <linux/kernel.h>
#include <linux/mfd/core.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <asm/olpc.h>

#define DRV_NAME "cs5535-mfd"

enum cs5535_mfd_bars {
	SMB_BAR = 0,
	GPIO_BAR = 1,
	MFGPT_BAR = 2,
	PMS_BAR = 4,
	ACPI_BAR = 5,
	NR_BARS,
};

static struct resource cs5535_mfd_resources[NR_BARS];

static struct mfd_cell cs5535_mfd_cells[] = {
	{
		.name = "cs5535-smb",
		.num_resources = 1,
		.resources = &cs5535_mfd_resources[SMB_BAR],
	},
	{
		.name = "cs5535-gpio",
		.num_resources = 1,
		.resources = &cs5535_mfd_resources[GPIO_BAR],
	},
	{
		.name = "cs5535-mfgpt",
		.num_resources = 1,
		.resources = &cs5535_mfd_resources[MFGPT_BAR],
	},
	{
		.name = "cs5535-pms",
		.num_resources = 1,
		.resources = &cs5535_mfd_resources[PMS_BAR],
	},
};

static struct mfd_cell cs5535_olpc_mfd_cells[] = {
	{
		.name = "olpc-xo1-pm-acpi",
		.num_resources = 1,
		.resources = &cs5535_mfd_resources[ACPI_BAR],
	},
	{
		.name = "olpc-xo1-sci-acpi",
		.num_resources = 1,
		.resources = &cs5535_mfd_resources[ACPI_BAR],
	},
};

static int cs5535_mfd_probe(struct pci_dev *pdev,
		const struct pci_device_id *id)
{
	int err, bar;

	err = pci_enable_device(pdev);
	if (err)
		return err;

	for (bar = 0; bar < NR_BARS; bar++) {
		struct resource *r = &cs5535_mfd_resources[bar];

		r->flags = IORESOURCE_IO;
		r->start = pci_resource_start(pdev, bar);
		r->end = pci_resource_end(pdev, bar);
	}

	err = pci_request_region(pdev, PMS_BAR, DRV_NAME);
	if (err) {
		dev_err(&pdev->dev, "Failed to request PMS_BAR's IO region\n");
		goto err_disable;
	}

	err = mfd_add_devices(&pdev->dev, PLATFORM_DEVID_NONE, cs5535_mfd_cells,
			      ARRAY_SIZE(cs5535_mfd_cells), NULL, 0, NULL);
	if (err) {
		dev_err(&pdev->dev,
			"Failed to add CS5535 sub-devices: %d\n", err);
		goto err_release_pms;
	}

	if (machine_is_olpc()) {
		err = pci_request_region(pdev, ACPI_BAR, DRV_NAME);
		if (err) {
			dev_err(&pdev->dev,
				"Failed to request ACPI_BAR's IO region\n");
			goto err_remove_devices;
		}

		err = mfd_add_devices(&pdev->dev, PLATFORM_DEVID_NONE,
				      cs5535_olpc_mfd_cells,
				      ARRAY_SIZE(cs5535_olpc_mfd_cells),
				      NULL, 0, NULL);
		if (err) {
			dev_err(&pdev->dev,
				"Failed to add CS5535 OLPC sub-devices: %d\n",
				err);
			goto err_release_acpi;
		}
	}

	dev_info(&pdev->dev, "%zu devices registered.\n",
			ARRAY_SIZE(cs5535_mfd_cells));

	return 0;

err_release_acpi:
	pci_release_region(pdev, ACPI_BAR);
err_remove_devices:
	mfd_remove_devices(&pdev->dev);
err_release_pms:
	pci_release_region(pdev, PMS_BAR);
err_disable:
	pci_disable_device(pdev);
	return err;
}

static void cs5535_mfd_remove(struct pci_dev *pdev)
{
	mfd_remove_devices(&pdev->dev);

	if (machine_is_olpc())
		pci_release_region(pdev, ACPI_BAR);

	pci_release_region(pdev, PMS_BAR);
	pci_disable_device(pdev);
}

static const struct pci_device_id cs5535_mfd_pci_tbl[] = {
	{ PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_CS5535_ISA) },
	{ PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_CS5536_ISA) },
	{ 0, }
};
MODULE_DEVICE_TABLE(pci, cs5535_mfd_pci_tbl);

static struct pci_driver cs5535_mfd_driver = {
	.name = DRV_NAME,
	.id_table = cs5535_mfd_pci_tbl,
	.probe = cs5535_mfd_probe,
	.remove = cs5535_mfd_remove,
};

module_pci_driver(cs5535_mfd_driver);

MODULE_AUTHOR("Andres Salomon <dilinger@queued.net>");
MODULE_DESCRIPTION("MFD driver for CS5535/CS5536 southbridge's ISA PCI device");
MODULE_LICENSE("GPL");