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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
|
/*
* intel_byt_e.c - Baytrail EC interface driver
*
* Copyright (C) 2013 Intel Corporation
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* 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; version 2 of the License.
*
* 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.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Author: Ramakrishna Pallala <ramakrishna.pallala@intel.com>
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/jiffies.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/io.h>
#include <linux/slab.h>
#include <linux/pm_runtime.h>
#include <linux/interrupt.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/gpio.h>
#include <linux/delay.h>
#include <linux/notifier.h>
#include <linux/sched.h>
#include <linux/workqueue.h>
#include <linux/wait.h>
#include <linux/input.h>
#include <linux/acpi.h>
#include <linux/acpi_gpio.h>
#include <asm/intel_byt_ec.h>
#include <asm/intel_byt_buttons.h>
#define EC_SPACE_SIZE 256
/* EC status register */
#define BYT_EC_FLAG_OBF 0x01 /* Output buffer full */
#define BYT_EC_FLAG_IBF 0x02 /* Input buffer full */
#define BYT_EC_FLAG_IGN 0x04 /* Ignore the status */
#define BYT_EC_FLAG_CMD 0x08 /* Byte in data buffer is CMD */
#define BYT_EC_FLAG_BURST 0x10 /* burst mode */
#define BYT_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
#define BYT_EC_FLAG_SMI 0x40 /* EC-SMI occurred */
#define BYT_EC_FLAG_IGN1 0x80 /* Ignore the status */
#define BYT_EC_DELAY 500 /* Wait 500ms max. during EC ops */
#define BYT_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
#define BYT_EC_MSI_UDELAY 550 /* Wait 550us for MSI EC */
#define BYT_EC_MAX_SCI_QUEUE 32
static void byt_ec_add_devices(void);
static BLOCKING_NOTIFIER_HEAD(byt_ec_evt_notifier_list);
struct ec_chip_info {
struct platform_device *pdev;
struct mutex io_lock;
unsigned long gpio;
unsigned long cmd_addr;
unsigned long data_addr;
int irq;
wait_queue_head_t wait;
};
static struct ec_chip_info *chip_ptr;
static inline u8 ec_read_status(struct ec_chip_info *chip)
{
u8 x = inb(chip->cmd_addr);
return x;
}
static inline u8 ec_read_data(struct ec_chip_info *chip)
{
u8 x = inb(chip->data_addr);
return x;
}
static inline void ec_write_cmd(struct ec_chip_info *chip, u8 command)
{
outb(command, chip->cmd_addr);
}
static inline void ec_write_data(struct ec_chip_info *chip, u8 data)
{
outb(data, chip->data_addr);
}
static int ec_check_ibf0(struct ec_chip_info *chip)
{
u8 status = ec_read_status(chip);
return (status & BYT_EC_FLAG_IBF) == 0;
}
static int ec_wait_ibf0(struct ec_chip_info *chip)
{
unsigned long delay = jiffies + msecs_to_jiffies(BYT_EC_DELAY);
unsigned long min_delay = msecs_to_jiffies(10);
/* interrupt wait manually if GPE mode is not active */
while (time_before(jiffies, delay))
if (wait_event_timeout(chip->wait, ec_check_ibf0(chip),
min_delay))
return 0;
return -ETIMEDOUT;
}
static int ec_check_obf1(struct ec_chip_info *chip)
{
u8 status = ec_read_status(chip);
return (status & BYT_EC_FLAG_OBF) == BYT_EC_FLAG_OBF;
}
static int ec_wait_obf1(struct ec_chip_info *chip)
{
unsigned long delay = jiffies + msecs_to_jiffies(BYT_EC_DELAY);
unsigned long min_delay = msecs_to_jiffies(10);
/* interrupt wait manually if GPE mode is not active */
while (time_before(jiffies, delay))
if (wait_event_timeout(chip->wait, ec_check_obf1(chip),
min_delay))
return 0;
return -ETIMEDOUT;
}
static int byt_ec_query(struct ec_chip_info *chip, u8 *val)
{
int ret;
ec_write_cmd(chip, BYT_EC_COMMAND_QUERY);
ret = ec_wait_obf1(chip);
if (ret < 0) {
dev_err(&chip->pdev->dev, "EC tx/rx error%d\n", ret);
return ret;
}
*val = ec_read_data(chip);
return 0;
}
static int byt_ec_read8(struct ec_chip_info *chip, u8 addr, u8 *val)
{
int ret;
ec_write_cmd(chip, BYT_EC_COMMAND_READ);
ret = ec_wait_ibf0(chip);
if (ret < 0) {
dev_err(&chip->pdev->dev, "EC tx/rx error%d\n", ret);
return ret;
}
ec_write_data(chip, addr);
ret = ec_wait_obf1(chip);
if (ret < 0) {
dev_err(&chip->pdev->dev, "EC tx/rx error%d\n", ret);
return ret;
}
*val = ec_read_data(chip);
return 0;
}
static int byt_ec_write8(struct ec_chip_info *chip, u8 addr, u8 val)
{
int ret;
ec_write_cmd(chip, BYT_EC_COMMAND_WRITE);
ret = ec_wait_ibf0(chip);
if (ret < 0) {
dev_err(&chip->pdev->dev, "EC tx/rx error%d\n", ret);
return ret;
}
ec_write_data(chip, addr);
ret = ec_wait_ibf0(chip);
if (ret < 0) {
dev_err(&chip->pdev->dev, "EC tx/rx error%d\n", ret);
return ret;
}
ec_write_data(chip, val);
ret = ec_wait_ibf0(chip);
if (ret < 0) {
dev_err(&chip->pdev->dev, "EC tx/rx error%d\n", ret);
return ret;
}
return 0;
}
int byt_ec_send_cmd(u8 command)
{
int ret;
if (!chip_ptr)
return -EINVAL;
mutex_lock(&chip_ptr->io_lock);
ec_write_cmd(chip_ptr, command);
ret = ec_wait_ibf0(chip_ptr);
if (ret < 0)
dev_err(&chip_ptr->pdev->dev, "EC tx/rx error%d\n", ret);
mutex_unlock(&chip_ptr->io_lock);
return ret;
}
EXPORT_SYMBOL(byt_ec_send_cmd);
int byt_ec_read_byte(u8 addr, u8 *val)
{
int err;
u8 temp_data;
if (!chip_ptr)
return -ENODEV;
mutex_lock(&chip_ptr->io_lock);
err = byt_ec_read8(chip_ptr, addr, &temp_data);
mutex_unlock(&chip_ptr->io_lock);
if (!err) {
*val = temp_data;
return 0;
} else
return err;
}
EXPORT_SYMBOL(byt_ec_read_byte);
int byt_ec_write_byte(u8 addr, u8 val)
{
int err;
if (!chip_ptr)
return -ENODEV;
mutex_lock(&chip_ptr->io_lock);
err = byt_ec_write8(chip_ptr, addr, val);
mutex_unlock(&chip_ptr->io_lock);
return err;
}
EXPORT_SYMBOL(byt_ec_write_byte);
int byt_ec_read_word(u8 addr, u16 *val)
{
int err;
u8 temp_data;
if (!chip_ptr)
return -ENODEV;
mutex_lock(&chip_ptr->io_lock);
/* read byte 0 */
err = byt_ec_read8(chip_ptr, addr, &temp_data);
if (err)
goto rd_word_err;
*val = temp_data;
/* read byte 1 */
err = byt_ec_read8(chip_ptr, addr + 1, &temp_data);
if (err)
goto rd_word_err;
*val |= ((u16)temp_data << 8);
mutex_unlock(&chip_ptr->io_lock);
return 0;
rd_word_err:
mutex_unlock(&chip_ptr->io_lock);
return err;
}
EXPORT_SYMBOL(byt_ec_read_word);
int byt_ec_write_word(u8 addr, u16 val)
{
int err;
if (!chip_ptr)
return -ENODEV;
mutex_lock(&chip_ptr->io_lock);
/* write byte 0 */
err = byt_ec_write8(chip_ptr, addr, (val & 0xff));
if (err)
goto wr_word_err;
/* write byte 1 */
err = byt_ec_write8(chip_ptr, addr, ((val >> 8) & 0xff));
if (err)
goto wr_word_err;
mutex_unlock(&chip_ptr->io_lock);
return 0;
wr_word_err:
mutex_unlock(&chip_ptr->io_lock);
return err;
}
EXPORT_SYMBOL(byt_ec_write_word);
static irqreturn_t ec_intr_thread_handler(int id, void *dev)
{
struct ec_chip_info *chip = dev;
int ret, i;
u8 val = 0, stat;
stat = ec_read_status(chip);
if (stat & BYT_EC_FLAG_OBF) {
dev_dbg(&chip->pdev->dev, "OBF event\n");
wake_up(&chip->wait);
}
if (stat & BYT_EC_FLAG_IBF) {
dev_dbg(&chip->pdev->dev, "IBF event\n");
wake_up(&chip->wait);
}
if (stat & BYT_EC_FLAG_CMD)
dev_dbg(&chip->pdev->dev, "CMD event\n");
if (stat & BYT_EC_FLAG_BURST)
dev_dbg(&chip->pdev->dev, "Burst event\n");
if (stat & BYT_EC_FLAG_SMI)
dev_dbg(&chip->pdev->dev, "SMI event\n");
/* query event */
for (i = 0; i < BYT_EC_MAX_SCI_QUEUE; i++) {
if ((ec_read_status(chip) & BYT_EC_FLAG_SCI)) {
mutex_lock(&chip->io_lock);
ret = byt_ec_query(chip, &val);
mutex_unlock(&chip->io_lock);
/* Handle SCI query code */
if (!ret)
blocking_notifier_call_chain(
&byt_ec_evt_notifier_list, val, chip);
dev_info(&chip->pdev->dev, "query stat:%x\n", val);
continue;
}
break;
}
return IRQ_HANDLED;
}
static void byt_enable_acpi_mode(struct ec_chip_info *chip)
{
ec_write_cmd(chip, BYT_EC_ACPI_ENABLE);
/* add 100mSec delay */
mdelay(100);
return;
}
static void byt_disable_acpi_mode(struct ec_chip_info *chip)
{
ec_write_cmd(chip, BYT_EC_ACPI_DISABLE);
/* add 100mSec delay */
mdelay(100);
return;
}
/* EC buttons platform data */
static struct byt_keys_button byt_m_nrpt_buttons[] = {
{ KEY_POWER, EV_KEY, "Power_btn", 1 },
{ },
};
static struct byt_keys_button byt_m_rpt_buttons[] = {
{ KEY_VOLUMEUP, EV_KEY, "Volume_up", 1 },
{ KEY_VOLUMEDOWN, EV_KEY, "Volume_down", 1 },
{ KEY_HOME, EV_KEY, "Home_btn", 1 },
};
static struct byt_keys_platform_data byt_key_pdata[2] = {
{
.buttons = byt_m_nrpt_buttons,
.nbuttons = 1,
.rep = 0,
}, {
.buttons = byt_m_rpt_buttons,
.nbuttons = 3,
.rep = 1,
},
};
static int byt_ec_create_device(const char *name, void *pdata)
{
int ret = 0;
struct platform_device *pdev = NULL;
pdev = platform_device_alloc(name, -1);
if (!pdev) {
pr_err("out of memory for platform dev %s\n", name);
goto dev_add_error;
}
pdev->dev.platform_data = pdata;
ret = platform_device_add(pdev);
if (ret) {
pr_err("failed to add %s platform device\n", name);
platform_device_put(pdev);
}
dev_add_error:
return ret;
}
static void byt_ec_add_devices()
{
/* Add battery device */
byt_ec_create_device("ec_battery", NULL);
/* Add button devices */
byt_ec_create_device("byt_m_nrpt_btns", &byt_key_pdata[0]);
byt_ec_create_device("byt_m_rpt_btns", &byt_key_pdata[1]);
/* Add thermal device bellow */
return;
}
void byt_ec_evt_register_notify(struct notifier_block *nb)
{
blocking_notifier_chain_register(&byt_ec_evt_notifier_list, nb);
}
EXPORT_SYMBOL(byt_ec_evt_register_notify);
void byt_ec_evt_unregister_notify(struct notifier_block *nb)
{
blocking_notifier_chain_unregister(&byt_ec_evt_notifier_list, nb);
}
EXPORT_SYMBOL(byt_ec_evt_unregister_notify);
static int byt_ec_probe(struct platform_device *pdev)
{
struct ec_chip_info *chip;
int ret;
u8 val, stat;
chip = kzalloc(sizeof(*chip), GFP_KERNEL);
if (!chip) {
dev_err(&pdev->dev, "mem alloc failed\n");
return -ENOMEM;
}
chip->pdev = pdev;
platform_set_drvdata(pdev, chip);
mutex_init(&chip->io_lock);
init_waitqueue_head(&chip->wait);
chip->gpio = acpi_get_gpio_by_index(&pdev->dev, 0, NULL);
chip->data_addr = 0x62;
chip->cmd_addr = 0x66;
if (!request_region(chip->data_addr, 1, "ec-data")) {
dev_err(&pdev->dev, "Could not request io port 0x%lx\n",
chip->data_addr);
kfree(chip);
return -EBUSY;
}
if (!request_region(chip->cmd_addr, 1, "ec-cmd")) {
dev_err(&pdev->dev, "Could not request io port 0x%lx\n",
chip->cmd_addr);
release_region(chip->data_addr, 1);
kfree(chip);
return -EBUSY;
}
/* enable EC acpi mode */
byt_enable_acpi_mode(chip);
/* read status register */
stat = ec_read_status(chip);
/* clear pending data */
if (stat & BYT_EC_FLAG_OBF)
ret = ec_read_data(chip);
/* clear pending event */
if (stat & BYT_EC_FLAG_SCI) {
ret = byt_ec_query(chip, &val);
if (!ret)
dev_info(&chip->pdev->dev, "query stat:%x\n", val);
else
dev_info(&chip->pdev->dev, "query err:%d\n", ret);
}
chip->irq = gpio_to_irq(chip->gpio);
ret = request_threaded_irq(chip->irq, NULL,
ec_intr_thread_handler,
IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
"byt-ec", chip);
if (ret) {
dev_warn(&chip->pdev->dev,
"cannot get IRQ:%d\n", chip->irq);
chip->irq = -1;
} else {
dev_info(&chip->pdev->dev, "IRQ No:%d\n", chip->irq);
}
chip_ptr = chip;
byt_ec_add_devices();
return 0;
}
static int byt_ec_remove(struct platform_device *pdev)
{
struct ec_chip_info *chip = platform_get_drvdata(pdev);
/* disable EC acpi mode */
byt_disable_acpi_mode(chip);
free_irq(chip->irq, chip);
release_region(chip->cmd_addr, 1);
release_region(chip->data_addr, 1);
kfree(chip);
return 0;
}
static struct acpi_device_id byt_ec_acpi_match[] = {
{ "BYTEC001", 0 },
{ },
};
MODULE_DEVICE_TABLE(acpi, byt_ec_acpi_match);
static struct platform_driver byt_ec_driver = {
.probe = byt_ec_probe,
.remove = byt_ec_remove,
.driver = {
.name = "byt-ec",
.acpi_match_table = ACPI_PTR(byt_ec_acpi_match),
},
};
static int __init byt_ec_init(void)
{
int ret = 0;
ret = platform_driver_register(&byt_ec_driver);
if (ret < 0) {
pr_err("platform driver reg failed %s\n",
"byt-ec");
return ret;
}
return 0;
}
fs_initcall(byt_ec_init);
static void __exit byt_ec_exit(void)
{
platform_driver_unregister(&byt_ec_driver);
}
module_exit(byt_ec_exit);
MODULE_AUTHOR("Ramakrishna Pallala <ramakrishna.pallala@intel.com>");
MODULE_DESCRIPTION("Baytrail EC Battery Driver");
MODULE_LICENSE("GPL");
|