summaryrefslogtreecommitdiffstats
path: root/drivers/platform/x86/intel_oaktrail.c
blob: 79a0c2f6be53996c2771ffd532e517a6ac8bef4e (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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
 * intel_oaktrail.c - Intel OakTrail Platform support.
 *
 * Copyright (C) 2010-2011 Intel Corporation
 * Author: Yin Kangkai (kangkai.yin@intel.com)
 *
 * based on Compal driver, Copyright (C) 2008 Cezary Jackiewicz
 * <cezary.jackiewicz (at) gmail.com>, based on MSI driver
 * Copyright (C) 2006 Lennart Poettering <mzxreary (at) 0pointer (dot) de>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 *  02110-1301, USA.
 *
 * This driver does below things:
 * 1. registers itself in the Linux backlight control in
 *    /sys/class/backlight/intel_oaktrail/
 *
 * 2. registers in the rfkill subsystem here: /sys/class/rfkill/rfkillX/
 *    for these components: wifi, bluetooth, wwan (3g), gps
 *
 * This driver might work on other products based on Oaktrail. If you
 * want to try it you can pass force=1 as argument to the module which
 * will force it to load even when the DMI data doesn't identify the
 * product as compatible.
 */

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/acpi.h>
#include <linux/fb.h>
#include <linux/mutex.h>
#include <linux/err.h>
#include <linux/i2c.h>
#include <linux/backlight.h>
#include <linux/platform_device.h>
#include <linux/dmi.h>
#include <linux/rfkill.h>
#include <acpi/acpi_bus.h>
#include <acpi/acpi_drivers.h>


#define DRIVER_NAME	"intel_oaktrail"
#define DRIVER_VERSION	"0.4ac1"

/*
 * This is the devices status address in EC space, and the control bits
 * definition:
 *
 * (1 << 0):	Camera enable/disable, RW.
 * (1 << 1):	Bluetooth enable/disable, RW.
 * (1 << 2):	GPS enable/disable, RW.
 * (1 << 3):	WiFi enable/disable, RW.
 * (1 << 4):	WWAN (3G) enable/disalbe, RW.
 * (1 << 5):	Touchscreen enable/disable, Read Only.
 */
#define OT_EC_DEVICE_STATE_ADDRESS	0xD6

#define OT_EC_CAMERA_MASK	(1 << 0)
#define OT_EC_BT_MASK		(1 << 1)
#define OT_EC_GPS_MASK		(1 << 2)
#define OT_EC_WIFI_MASK		(1 << 3)
#define OT_EC_WWAN_MASK		(1 << 4)
#define OT_EC_TS_MASK		(1 << 5)

/*
 * This is the address in EC space and commands used to control LCD backlight:
 *
 * Two steps needed to change the LCD backlight:
 *   1. write the backlight percentage into OT_EC_BL_BRIGHTNESS_ADDRESS;
 *   2. write OT_EC_BL_CONTROL_ON_DATA into OT_EC_BL_CONTROL_ADDRESS.
 *
 * To read the LCD back light, just read out the value from
 * OT_EC_BL_BRIGHTNESS_ADDRESS.
 *
 * LCD backlight brightness range: 0 - 100 (OT_EC_BL_BRIGHTNESS_MAX)
 */
#define OT_EC_BL_BRIGHTNESS_ADDRESS	0x44
#define OT_EC_BL_BRIGHTNESS_MAX		100
#define OT_EC_BL_CONTROL_ADDRESS	0x3A
#define OT_EC_BL_CONTROL_ON_DATA	0x1A


static bool force;
module_param(force, bool, 0);
MODULE_PARM_DESC(force, "Force driver load, ignore DMI data");

static struct platform_device *oaktrail_device;
static struct backlight_device *oaktrail_bl_device;
static struct rfkill *bt_rfkill;
static struct rfkill *gps_rfkill;
static struct rfkill *wifi_rfkill;
static struct rfkill *wwan_rfkill;


/* rfkill */
static int oaktrail_rfkill_set(void *data, bool blocked)
{
	u8 value;
	u8 result;
	unsigned long radio = (unsigned long) data;

	ec_read(OT_EC_DEVICE_STATE_ADDRESS, &result);

	if (!blocked)
		value = (u8) (result | radio);
	else
		value = (u8) (result & ~radio);

	ec_write(OT_EC_DEVICE_STATE_ADDRESS, value);

	return 0;
}

static const struct rfkill_ops oaktrail_rfkill_ops = {
	.set_block = oaktrail_rfkill_set,
};

static struct rfkill *oaktrail_rfkill_new(char *name, enum rfkill_type type,
					  unsigned long mask)
{
	struct rfkill *rfkill_dev;
	u8 value;
	int err;

	rfkill_dev = rfkill_alloc(name, &oaktrail_device->dev, type,
				  &oaktrail_rfkill_ops, (void *)mask);
	if (!rfkill_dev)
		return ERR_PTR(-ENOMEM);

	ec_read(OT_EC_DEVICE_STATE_ADDRESS, &value);
	rfkill_init_sw_state(rfkill_dev, (value & mask) != 1);

	err = rfkill_register(rfkill_dev);
	if (err) {
		rfkill_destroy(rfkill_dev);
		return ERR_PTR(err);
	}

	return rfkill_dev;
}

static inline void __oaktrail_rfkill_cleanup(struct rfkill *rf)
{
	if (rf) {
		rfkill_unregister(rf);
		rfkill_destroy(rf);
	}
}

static void oaktrail_rfkill_cleanup(void)
{
	__oaktrail_rfkill_cleanup(wifi_rfkill);
	__oaktrail_rfkill_cleanup(bt_rfkill);
	__oaktrail_rfkill_cleanup(gps_rfkill);
	__oaktrail_rfkill_cleanup(wwan_rfkill);
}

static int oaktrail_rfkill_init(void)
{
	int ret;

	wifi_rfkill = oaktrail_rfkill_new("oaktrail-wifi",
					  RFKILL_TYPE_WLAN,
					  OT_EC_WIFI_MASK);
	if (IS_ERR(wifi_rfkill)) {
		ret = PTR_ERR(wifi_rfkill);
		wifi_rfkill = NULL;
		goto cleanup;
	}

	bt_rfkill = oaktrail_rfkill_new("oaktrail-bluetooth",
					RFKILL_TYPE_BLUETOOTH,
					OT_EC_BT_MASK);
	if (IS_ERR(bt_rfkill)) {
		ret = PTR_ERR(bt_rfkill);
		bt_rfkill = NULL;
		goto cleanup;
	}

	gps_rfkill = oaktrail_rfkill_new("oaktrail-gps",
					 RFKILL_TYPE_GPS,
					 OT_EC_GPS_MASK);
	if (IS_ERR(gps_rfkill)) {
		ret = PTR_ERR(gps_rfkill);
		gps_rfkill = NULL;
		goto cleanup;
	}

	wwan_rfkill = oaktrail_rfkill_new("oaktrail-wwan",
					  RFKILL_TYPE_WWAN,
					  OT_EC_WWAN_MASK);
	if (IS_ERR(wwan_rfkill)) {
		ret = PTR_ERR(wwan_rfkill);
		wwan_rfkill = NULL;
		goto cleanup;
	}

	return 0;

cleanup:
	oaktrail_rfkill_cleanup();
	return ret;
}


/* backlight */
static int get_backlight_brightness(struct backlight_device *b)
{
	u8 value;
	ec_read(OT_EC_BL_BRIGHTNESS_ADDRESS, &value);

	return value;
}

static int set_backlight_brightness(struct backlight_device *b)
{
	u8 percent = (u8) b->props.brightness;
	if (percent < 0 || percent > OT_EC_BL_BRIGHTNESS_MAX)
		return -EINVAL;

	ec_write(OT_EC_BL_BRIGHTNESS_ADDRESS, percent);
	ec_write(OT_EC_BL_CONTROL_ADDRESS, OT_EC_BL_CONTROL_ON_DATA);

	return 0;
}

static const struct backlight_ops oaktrail_bl_ops = {
	.get_brightness = get_backlight_brightness,
	.update_status	= set_backlight_brightness,
};

static int oaktrail_backlight_init(void)
{
	struct backlight_device *bd;
	struct backlight_properties props;

	memset(&props, 0, sizeof(struct backlight_properties));
	props.type = BACKLIGHT_PLATFORM;
	props.max_brightness = OT_EC_BL_BRIGHTNESS_MAX;
	bd = backlight_device_register(DRIVER_NAME,
				       &oaktrail_device->dev, NULL,
				       &oaktrail_bl_ops,
				       &props);

	if (IS_ERR(bd)) {
		oaktrail_bl_device = NULL;
		pr_warning("Unable to register backlight device\n");
		return PTR_ERR(bd);
	}

	oaktrail_bl_device = bd;

	bd->props.brightness = get_backlight_brightness(bd);
	bd->props.power = FB_BLANK_UNBLANK;
	backlight_update_status(bd);

	return 0;
}

static void oaktrail_backlight_exit(void)
{
	if (oaktrail_bl_device)
		backlight_device_unregister(oaktrail_bl_device);
}

static int __devinit oaktrail_probe(struct platform_device *pdev)
{
	return 0;
}

static int __devexit oaktrail_remove(struct platform_device *pdev)
{
	return 0;
}

static struct platform_driver oaktrail_driver = {
	.driver = {
		.name = DRIVER_NAME,
		.owner = THIS_MODULE,
	},
	.probe	= oaktrail_probe,
	.remove	= __devexit_p(oaktrail_remove)
};

static int dmi_check_cb(const struct dmi_system_id *id)
{
	pr_info("Identified model '%s'\n", id->ident);
	return 0;
}

static struct dmi_system_id __initdata oaktrail_dmi_table[] = {
	{
		.ident = "OakTrail platform",
		.matches = {
			DMI_MATCH(DMI_PRODUCT_NAME, "OakTrail platform"),
		},
		.callback = dmi_check_cb
	},
	{ }
};
MODULE_DEVICE_TABLE(dmi, oaktrail_dmi_table);

static int __init oaktrail_init(void)
{
	int ret;

	if (acpi_disabled) {
		pr_err("ACPI needs to be enabled for this driver to work!\n");
		return -ENODEV;
	}

	if (!force && !dmi_check_system(oaktrail_dmi_table)) {
		pr_err("Platform not recognized (You could try the module's force-parameter)");
		return -ENODEV;
	}

	ret = platform_driver_register(&oaktrail_driver);
	if (ret) {
		pr_warning("Unable to register platform driver\n");
		goto err_driver_reg;
	}

	oaktrail_device = platform_device_alloc(DRIVER_NAME, -1);
	if (!oaktrail_device) {
		pr_warning("Unable to allocate platform device\n");
		ret = -ENOMEM;
		goto err_device_alloc;
	}

	ret = platform_device_add(oaktrail_device);
	if (ret) {
		pr_warning("Unable to add platform device\n");
		goto err_device_add;
	}

	if (!acpi_video_backlight_support()) {
		ret = oaktrail_backlight_init();
		if (ret)
			goto err_backlight;

	} else
		pr_info("Backlight controlled by ACPI video driver\n");

	ret = oaktrail_rfkill_init();
	if (ret) {
		pr_warning("Setup rfkill failed\n");
		goto err_rfkill;
	}

	pr_info("Driver "DRIVER_VERSION" successfully loaded\n");
	return 0;

err_rfkill:
	oaktrail_backlight_exit();
err_backlight:
	platform_device_del(oaktrail_device);
err_device_add:
	platform_device_put(oaktrail_device);
err_device_alloc:
	platform_driver_unregister(&oaktrail_driver);
err_driver_reg:

	return ret;
}

static void __exit oaktrail_cleanup(void)
{
	oaktrail_backlight_exit();
	oaktrail_rfkill_cleanup();
	platform_device_unregister(oaktrail_device);
	platform_driver_unregister(&oaktrail_driver);

	pr_info("Driver unloaded\n");
}

module_init(oaktrail_init);
module_exit(oaktrail_cleanup);

MODULE_AUTHOR("Yin Kangkai (kangkai.yin@intel.com)");
MODULE_DESCRIPTION("Intel Oaktrail Platform ACPI Extras");
MODULE_VERSION(DRIVER_VERSION);
MODULE_LICENSE("GPL");