summaryrefslogtreecommitdiff
path: root/arch/arm/mach-ux500/board-mop500-uib.c
blob: 908c5d973daf88e3e8ca947d714cbeb0dec53431 (plain)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*

 * Copyright (C) ST-Ericsson SA 2010
 *
 * Author: Rabin Vincent <rabin.vincent@stericsson.com> for ST-Ericsson
 * License terms: GNU General Public License (GPL), version 2
 */

#define pr_fmt(fmt)	"mop500-uib: " fmt

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/i2c.h>
#include <linux/platform_device.h>
#include <linux/input.h>
#include <linux/gpio_keys.h>
#include <linux/regulator/consumer.h>

#include <mach/hardware.h>
#include <asm/mach-types.h>

#include "pins.h"
#include "board-mop500.h"

enum mop500_uib {
	STUIB,
	U8500UIB,
	U8500UIB_R3,
	NO_UIB,
};

struct uib {
	const char *name;
	const char *option;
	void (*init)(void);
};

static u8 type_of_uib = NO_UIB;

static struct uib __initdata mop500_uibs[] = {
	[STUIB] = {
		.name	= "ST-UIB",
		.option	= "stuib",
		.init	= mop500_stuib_init,
	},
	[U8500UIB] = {
		.name	= "U8500-UIB",
		.option	= "u8500uib",
		.init	= mop500_u8500uib_init,
	},
#ifdef CONFIG_TOUCHSCREEN_CYTTSP_SPI
	[U8500UIB_R3] = {
		.name   = "U8500-UIBR3",
		.option = "u8500uibr3",
		.init   = mop500_u8500uib_r3_init,
	},
#endif
};

static struct uib __initdata *mop500_uib;

static int __init mop500_uib_setup(char *str)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(mop500_uibs); i++) {
		struct uib *uib = &mop500_uibs[i];

		if (!strcmp(str, uib->option)) {
			mop500_uib = uib;
			break;
		}
	}

	if (i == ARRAY_SIZE(mop500_uibs))
		pr_err("invalid uib= option (%s)\n", str);

	return 1;
}
__setup("uib=", mop500_uib_setup);

/*
 * The UIBs are detected after the I2C host controllers are registered, so
 * i2c_register_board_info() can't be used.
 */
void mop500_uib_i2c_add(int busnum, struct i2c_board_info const *info,
		unsigned n)
{
	struct i2c_adapter *adap;
	struct i2c_client *client;
	int i;

	adap = i2c_get_adapter(busnum);
	if (!adap) {
		pr_err("failed to get adapter i2c%d\n", busnum);
		return;
	}

	for (i = 0; i < n; i++) {
		client = i2c_new_device(adap, &info[i]);
		if (!client)
			pr_err("failed to register %s to i2c%d\n",
					info[i].type, busnum);
	}

	i2c_put_adapter(adap);
}

static void __init __mop500_uib_init(struct uib *uib, const char *why)
{
	pr_info("%s (%s)\n", uib->name, why);

	if (strcmp("stuib", uib->option) == 0)
		type_of_uib = STUIB;
	else if (strcmp("u8500uib", uib->option) == 0)
		type_of_uib = U8500UIB;
	else if (strcmp("u8500uibr3", uib->option) == 0)
		type_of_uib = U8500UIB_R3;

	uib->init();
}

int uib_is_stuib(void)
{
	return (type_of_uib == STUIB);
}

int uib_is_u8500uib(void)
{
	return (type_of_uib == U8500UIB);
}

int uib_is_u8500uibr3(void)
{
	return (type_of_uib == U8500UIB_R3);
}


#ifdef CONFIG_UX500_GPIO_KEYS
static struct gpio_keys_button mop500_gpio_keys[] = {
	{
		.desc			= "SFH7741 Proximity Sensor",
		.type			= EV_SW,
		.code			= SW_FRONT_PROXIMITY,
		.active_low		= 0,
		.can_disable		= 1,
	},
	{
		.desc			= "HED54XXU11 Hall Effect Sensor",
		.type			= EV_SW,
		.code			= SW_LID, /* FIXME arbitrary usage */
		.active_low		= 0,
		.can_disable		= 1,
	}
};

static struct regulator *gpio_keys_regulator;
static int mop500_gpio_keys_activate(struct device *dev);
static void mop500_gpio_keys_deactivate(struct device *dev);

static struct gpio_keys_platform_data mop500_gpio_keys_data = {
	.buttons	= mop500_gpio_keys,
	.nbuttons	= ARRAY_SIZE(mop500_gpio_keys),
	.enable		= mop500_gpio_keys_activate,
	.disable	= mop500_gpio_keys_deactivate,
};

static struct platform_device mop500_gpio_keys_device = {
	.name	= "gpio-keys",
	.id	= 0,
	.dev	= {
		.platform_data	= &mop500_gpio_keys_data,
	},
};

static int mop500_gpio_keys_activate(struct device *dev)
{
	gpio_keys_regulator = regulator_get(&mop500_gpio_keys_device.dev,
						"vcc");
	if (IS_ERR(gpio_keys_regulator)) {
		dev_err(&mop500_gpio_keys_device.dev, "no regulator\n");
		return PTR_ERR(gpio_keys_regulator);
	}
	regulator_enable(gpio_keys_regulator);

	/*
	 * Please be aware that the start-up time of the SFH7741 is
	 * 120 ms and during that time the output is undefined.
	 */

	return 0;
}

static void mop500_gpio_keys_deactivate(struct device *dev)
{
	if (!IS_ERR(gpio_keys_regulator)) {
		regulator_disable(gpio_keys_regulator);
		regulator_put(gpio_keys_regulator);
	}
}

static __init void mop500_gpio_keys_init(void)
{
	struct ux500_pins *gpio_keys_pins = ux500_pins_get("gpio-keys.0");

	if (gpio_keys_pins == NULL) {
		pr_err("gpio_keys: Fail to get pins\n");
		return;
	}

	ux500_pins_enable(gpio_keys_pins);
	if (type_of_uib == U8500UIB_R3)
		mop500_gpio_keys[0].gpio = PIN_NUM(gpio_keys_pins->cfg[2]);
	else
		mop500_gpio_keys[0].gpio = PIN_NUM(gpio_keys_pins->cfg[0]);
	mop500_gpio_keys[1].gpio = PIN_NUM(gpio_keys_pins->cfg[1]);
}
#else
static inline void mop500_gpio_keys_init(void) { }
#endif

/* add any platform devices here - TODO */
static struct platform_device *mop500_uib_platform_devs[] __initdata = {
#ifdef CONFIG_UX500_GPIO_KEYS
	&mop500_gpio_keys_device,
#endif
};

/*
 * Detect the UIB attached based on the presence or absence of i2c devices.
 */
static int __init mop500_uib_init(void)
{
	struct uib *uib = mop500_uibs;
	struct i2c_adapter *i2c0;
	struct i2c_adapter *i2c3;
	int ret;

	/* snowball and non u8500 cpus dont have uib */
	if (!cpu_is_u8500() || machine_is_snowball())
		return -ENODEV;

	i2c0 = i2c_get_adapter(0);
	if (!i2c0) {
		__mop500_uib_init(&mop500_uibs[STUIB],
				"fallback, could not get i2c0");
		return -ENODEV;
	}

	/* U8500-UIB has the TC35893 at 0x44 on I2C0, the ST-UIB doesn't. */
	ret = i2c_smbus_xfer(i2c0, 0x44, 0, I2C_SMBUS_WRITE, 0,
			I2C_SMBUS_QUICK, NULL);
	i2c_put_adapter(i2c0);
	i2c3 = i2c_get_adapter(3);
	if (!i2c3) {
		__mop500_uib_init(&mop500_uibs[STUIB],
				"fallback, could not get i2c3");
		return -ENODEV;
	}

	if (ret == 0) {
		ret = i2c_smbus_xfer(i2c3, 0x4B, 0, I2C_SMBUS_WRITE, 0,
				I2C_SMBUS_QUICK, NULL);
		i2c_put_adapter(i2c3);
		if (ret == 0)
			uib = &mop500_uibs[U8500UIB];
		else
			uib = &mop500_uibs[U8500UIB_R3];
	} else {
		ret = i2c_smbus_xfer(i2c3, 0x5C, 0, I2C_SMBUS_WRITE, 0,
				I2C_SMBUS_QUICK, NULL);
		i2c_put_adapter(i2c3);
		if (ret == 0)
			uib = &mop500_uibs[STUIB];
	}
	__mop500_uib_init(uib, "detected");
	mop500_gpio_keys_init();
	platform_add_devices(mop500_uib_platform_devs,
					ARRAY_SIZE(mop500_uib_platform_devs));
	return 0;
}

module_init(mop500_uib_init);