summaryrefslogtreecommitdiffstats
path: root/drivers/platform/olpc/olpc-xo175-ec.c
blob: 5e1d14e35f20b19a154e028a5d5ca52166e2c066 (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
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
// SPDX-License-Identifier: GPL-2.0+
/*
 * Driver for the OLPC XO-1.75 Embedded Controller.
 *
 * The EC protocol is documented at:
 * http://wiki.laptop.org/go/XO_1.75_HOST_to_EC_Protocol
 *
 * Copyright (C) 2010 One Laptop per Child Foundation.
 * Copyright (C) 2018 Lubomir Rintel <lkundrak@v3.sk>
 */

#include <linux/completion.h>
#include <linux/ctype.h>
#include <linux/delay.h>
#include <linux/gpio/consumer.h>
#include <linux/input.h>
#include <linux/kfifo.h>
#include <linux/module.h>
#include <linux/olpc-ec.h>
#include <linux/platform_device.h>
#include <linux/power_supply.h>
#include <linux/reboot.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/spi/spi.h>

struct ec_cmd_t {
	u8 cmd;
	u8 bytes_returned;
};

enum ec_chan_t {
	CHAN_NONE = 0,
	CHAN_SWITCH,
	CHAN_CMD_RESP,
	CHAN_KEYBOARD,
	CHAN_TOUCHPAD,
	CHAN_EVENT,
	CHAN_DEBUG,
	CHAN_CMD_ERROR,
};

/*
 * EC events
 */
#define EVENT_AC_CHANGE			1  /* AC plugged/unplugged */
#define EVENT_BATTERY_STATUS		2  /* Battery low/full/error/gone */
#define EVENT_BATTERY_CRITICAL		3  /* Battery critical voltage */
#define EVENT_BATTERY_SOC_CHANGE	4  /* 1% SOC Change */
#define EVENT_BATTERY_ERROR		5  /* Abnormal error, query for cause */
#define EVENT_POWER_PRESSED		6  /* Power button was pressed */
#define EVENT_POWER_PRESS_WAKE		7  /* Woken up with a power button */
#define EVENT_TIMED_HOST_WAKE		8  /* Host wake timer */
#define EVENT_OLS_HIGH_LIMIT		9  /* OLS crossed dark threshold */
#define EVENT_OLS_LOW_LIMIT		10 /* OLS crossed light threshold */

/*
 * EC commands
 * (from http://dev.laptop.org/git/users/rsmith/ec-1.75/tree/ec_cmd.h)
 */
#define CMD_GET_API_VERSION		0x08 /* out: u8 */
#define CMD_READ_VOLTAGE		0x10 /* out: u16, *9.76/32, mV */
#define CMD_READ_CURRENT		0x11 /* out: s16, *15.625/120, mA */
#define CMD_READ_ACR			0x12 /* out: s16, *6250/15, uAh */
#define CMD_READ_BATT_TEMPERATURE	0x13 /* out: u16, *100/256, deg C */
#define CMD_READ_AMBIENT_TEMPERATURE	0x14 /* unimplemented, no hardware */
#define CMD_READ_BATTERY_STATUS		0x15 /* out: u8, bitmask */
#define CMD_READ_SOC			0x16 /* out: u8, percentage */
#define CMD_READ_GAUGE_ID		0x17 /* out: u8 * 8 */
#define CMD_READ_GAUGE_DATA		0x18 /* in: u8 addr, out: u8 data */
#define CMD_READ_BOARD_ID		0x19 /* out: u16 (platform id) */
#define CMD_READ_BATT_ERR_CODE		0x1f /* out: u8, error bitmask */
#define CMD_SET_DCON_POWER		0x26 /* in: u8 */
#define CMD_RESET_EC			0x28 /* none */
#define CMD_READ_BATTERY_TYPE		0x2c /* out: u8 */
#define CMD_SET_AUTOWAK			0x33 /* out: u8 */
#define CMD_SET_EC_WAKEUP_TIMER		0x36 /* in: u32, out: ? */
#define CMD_READ_EXT_SCI_MASK		0x37 /* ? */
#define CMD_WRITE_EXT_SCI_MASK		0x38 /* ? */
#define CMD_CLEAR_EC_WAKEUP_TIMER	0x39 /* none */
#define CMD_ENABLE_RUNIN_DISCHARGE	0x3B /* none */
#define CMD_DISABLE_RUNIN_DISCHARGE	0x3C /* none */
#define CMD_READ_MPPT_ACTIVE		0x3d /* out: u8 */
#define CMD_READ_MPPT_LIMIT		0x3e /* out: u8 */
#define CMD_SET_MPPT_LIMIT		0x3f /* in: u8 */
#define CMD_DISABLE_MPPT		0x40 /* none */
#define CMD_ENABLE_MPPT			0x41 /* none */
#define CMD_READ_VIN			0x42 /* out: u16 */
#define CMD_EXT_SCI_QUERY		0x43 /* ? */
#define RSP_KEYBOARD_DATA		0x48 /* ? */
#define RSP_TOUCHPAD_DATA		0x49 /* ? */
#define CMD_GET_FW_VERSION		0x4a /* out: u8 * 16 */
#define CMD_POWER_CYCLE			0x4b /* none */
#define CMD_POWER_OFF			0x4c /* none */
#define CMD_RESET_EC_SOFT		0x4d /* none */
#define CMD_READ_GAUGE_U16		0x4e /* ? */
#define CMD_ENABLE_MOUSE		0x4f /* ? */
#define CMD_ECHO			0x52 /* in: u8 * 5, out: u8 * 5 */
#define CMD_GET_FW_DATE			0x53 /* out: u8 * 16 */
#define CMD_GET_FW_USER			0x54 /* out: u8 * 16 */
#define CMD_TURN_OFF_POWER		0x55 /* none (same as 0x4c) */
#define CMD_READ_OLS			0x56 /* out: u16 */
#define CMD_OLS_SMT_LEDON		0x57 /* none */
#define CMD_OLS_SMT_LEDOFF		0x58 /* none */
#define CMD_START_OLS_ASSY		0x59 /* none */
#define CMD_STOP_OLS_ASSY		0x5a /* none */
#define CMD_OLS_SMTTEST_STOP		0x5b /* none */
#define CMD_READ_VIN_SCALED		0x5c /* out: u16 */
#define CMD_READ_BAT_MIN_W		0x5d /* out: u16 */
#define CMD_READ_BAR_MAX_W		0x5e /* out: u16 */
#define CMD_RESET_BAT_MINMAX_W		0x5f /* none */
#define CMD_READ_LOCATION		0x60 /* in: u16 addr, out: u8 data */
#define CMD_WRITE_LOCATION		0x61 /* in: u16 addr, u8 data */
#define CMD_KEYBOARD_CMD		0x62 /* in: u8, out: ? */
#define CMD_TOUCHPAD_CMD		0x63 /* in: u8, out: ? */
#define CMD_GET_FW_HASH			0x64 /* out: u8 * 16 */
#define CMD_SUSPEND_HINT		0x65 /* in: u8 */
#define CMD_ENABLE_WAKE_TIMER		0x66 /* in: u8 */
#define CMD_SET_WAKE_TIMER		0x67 /* in: 32 */
#define CMD_ENABLE_WAKE_AUTORESET	0x68 /* in: u8 */
#define CMD_OLS_SET_LIMITS		0x69 /* in: u16, u16 */
#define CMD_OLS_GET_LIMITS		0x6a /* out: u16, u16 */
#define CMD_OLS_SET_CEILING		0x6b /* in: u16 */
#define CMD_OLS_GET_CEILING		0x6c /* out: u16 */

/*
 * Accepted EC commands, and how many bytes they return. There are plenty
 * of EC commands that are no longer implemented, or are implemented only on
 * certain older boards.
 */
static const struct ec_cmd_t olpc_xo175_ec_cmds[] = {
	{ CMD_GET_API_VERSION, 1 },
	{ CMD_READ_VOLTAGE, 2 },
	{ CMD_READ_CURRENT, 2 },
	{ CMD_READ_ACR, 2 },
	{ CMD_READ_BATT_TEMPERATURE, 2 },
	{ CMD_READ_BATTERY_STATUS, 1 },
	{ CMD_READ_SOC, 1 },
	{ CMD_READ_GAUGE_ID, 8 },
	{ CMD_READ_GAUGE_DATA, 1 },
	{ CMD_READ_BOARD_ID, 2 },
	{ CMD_READ_BATT_ERR_CODE, 1 },
	{ CMD_SET_DCON_POWER, 0 },
	{ CMD_RESET_EC, 0 },
	{ CMD_READ_BATTERY_TYPE, 1 },
	{ CMD_ENABLE_RUNIN_DISCHARGE, 0 },
	{ CMD_DISABLE_RUNIN_DISCHARGE, 0 },
	{ CMD_READ_MPPT_ACTIVE, 1 },
	{ CMD_READ_MPPT_LIMIT, 1 },
	{ CMD_SET_MPPT_LIMIT, 0 },
	{ CMD_DISABLE_MPPT, 0 },
	{ CMD_ENABLE_MPPT, 0 },
	{ CMD_READ_VIN, 2 },
	{ CMD_GET_FW_VERSION, 16 },
	{ CMD_POWER_CYCLE, 0 },
	{ CMD_POWER_OFF, 0 },
	{ CMD_RESET_EC_SOFT, 0 },
	{ CMD_ECHO, 5 },
	{ CMD_GET_FW_DATE, 16 },
	{ CMD_GET_FW_USER, 16 },
	{ CMD_TURN_OFF_POWER, 0 },
	{ CMD_READ_OLS, 2 },
	{ CMD_OLS_SMT_LEDON, 0 },
	{ CMD_OLS_SMT_LEDOFF, 0 },
	{ CMD_START_OLS_ASSY, 0 },
	{ CMD_STOP_OLS_ASSY, 0 },
	{ CMD_OLS_SMTTEST_STOP, 0 },
	{ CMD_READ_VIN_SCALED, 2 },
	{ CMD_READ_BAT_MIN_W, 2 },
	{ CMD_READ_BAR_MAX_W, 2 },
	{ CMD_RESET_BAT_MINMAX_W, 0 },
	{ CMD_READ_LOCATION, 1 },
	{ CMD_WRITE_LOCATION, 0 },
	{ CMD_GET_FW_HASH, 16 },
	{ CMD_SUSPEND_HINT, 0 },
	{ CMD_ENABLE_WAKE_TIMER, 0 },
	{ CMD_SET_WAKE_TIMER, 0 },
	{ CMD_ENABLE_WAKE_AUTORESET, 0 },
	{ CMD_OLS_SET_LIMITS, 0 },
	{ CMD_OLS_GET_LIMITS, 4 },
	{ CMD_OLS_SET_CEILING, 0 },
	{ CMD_OLS_GET_CEILING, 2 },
	{ CMD_READ_EXT_SCI_MASK, 2 },
	{ CMD_WRITE_EXT_SCI_MASK, 0 },

	{ }
};

#define EC_MAX_CMD_DATA_LEN	5
#define EC_MAX_RESP_LEN		16

#define LOG_BUF_SIZE		128

#define PM_WAKEUP_TIME		1000

#define EC_ALL_EVENTS		GENMASK(15, 0)

enum ec_state_t {
	CMD_STATE_IDLE = 0,
	CMD_STATE_WAITING_FOR_SWITCH,
	CMD_STATE_CMD_IN_TX_FIFO,
	CMD_STATE_CMD_SENT,
	CMD_STATE_RESP_RECEIVED,
	CMD_STATE_ERROR_RECEIVED,
};

struct olpc_xo175_ec_cmd {
	u8 command;
	u8 nr_args;
	u8 data_len;
	u8 args[EC_MAX_CMD_DATA_LEN];
};

struct olpc_xo175_ec_resp {
	u8 channel;
	u8 byte;
};

struct olpc_xo175_ec {
	bool suspended;

	/* SPI related stuff. */
	struct spi_device *spi;
	struct spi_transfer xfer;
	struct spi_message msg;
	union {
		struct olpc_xo175_ec_cmd cmd;
		struct olpc_xo175_ec_resp resp;
	} tx_buf, rx_buf;

	/* GPIO for the CMD signals. */
	struct gpio_desc *gpio_cmd;

	/* Command handling related state. */
	spinlock_t cmd_state_lock;
	int cmd_state;
	bool cmd_running;
	struct completion cmd_done;
	struct olpc_xo175_ec_cmd cmd;
	u8 resp_data[EC_MAX_RESP_LEN];
	int expected_resp_len;
	int resp_len;

	/* Power button. */
	struct input_dev *pwrbtn;

	/* Debug handling. */
	char logbuf[LOG_BUF_SIZE];
	int logbuf_len;
};

static struct platform_device *olpc_ec;

static int olpc_xo175_ec_resp_len