i2c: ChromeOS EC tunnel driver

Add a driver to tunnel I2C over to the ChromeOS EC.

BUG=chrome-os-partner:19249
TEST=Use i2cdetect/i2cset/i2cdump to access tps65090

localhost ~ # i2cdetect -y -a -r 20
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

localhost ~ # i2cdump -f -y 20 0x48
No size specified (using byte-data access)
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f    0123456789abcdef
00: 00 00 3e 00 12 00 0b 1f 1f ff 20 00 1e 1e 1e 1f    ..>.?.???. .????
10: 1f 1f 0e 1f 1f 0e 20 00 00 07 00 00 00 00 00 00    ?????? ..?......
20: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................

localhost ~ # i2cset -f -y 20 0x48 0x10 0
localhost ~ # i2cdump -f -y 20 0x48
No size specified (using byte-data access)
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f    0123456789abcdef
00: 00 00 3e 00 12 00 0b 1f 1f ff 20 00 1e 1e 1e 1f    ..>.?.???. .????
10: 00 1f 0e 1f 1f 0e 20 00 00 07 00 00 00 00 00 00    .????? ..?......
20: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................

localhost ~ # i2cset -f -y 20 0x48 0x10 0x1f
localhost ~ # i2cdump -f -y 20 0x48
No size specified (using byte-data access)
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f    0123456789abcdef
00: 00 00 3e 00 12 00 0b 1f 1f ff 20 00 1e 1e 1e 1f    ..>.?.???. .????
10: 1f 1f 0e 1f 1f 0e 20 00 00 07 00 00 00 00 00 00    ?????? ..?......

TEST=Use pydevi2c to stress things out:
  # python -i devi2c.py
  >>> tps = I2CDevice(20, 0x48, force=True)
  >>> for i in xrange(251): tps.Get(0, i)
...and get no errors

Change-Id: Ia0bb4764da173741c8242fa52eb6cfc26e5c846b
Original-Change-Id:  I0c7844ea81a5e48c831abd34f2e6eab496837dab
Signed-off-by: Vincent Palatin <vpalatin@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Doug Anderson <dianders@chromium.org>
Reviewed-by: Simon Glass <sjg@google.com>
Tested-by: Doug Anderson <dianders@google.com>
Commit-Queue: Doug Anderson <dianders@google.com>
(cherry picked from commit 8cae1d7503bafbfdc98491b1e337f8f732672ba1)
Reviewed-on: https://gerrit.chromium.org/gerrit/56519

Conflicts:

	drivers/i2c/busses/Makefile
CHROMIUM: i2c: Use subsys_initcall for i2c-cros-ec-tunnel

It shouldn't be necessary to init so early.  ...but...

It's really convenient.  For details, see:
  I2dc07b1edfff911fcb94a468d058c5d36ea17594
  85c24d5 CHROMIUM: i2c: mux: Use subsys_initcall for the i2c-arb-gpio-challenge

BUG=chromium:239659
TEST=Boot and see tps65090 init earlier in the sequence.

Change-Id: Iefe741ccbc2723f2241917313de3150bc7a782fc
Signed-off-by: Doug Anderson <dianders@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/57042
Commit-Queue: Simon Glass <sjg@chromium.org>
Reviewed-by: Simon Glass <sjg@chromium.org>
CHROMIUM: cros_ec: IRQs for cros_ec should be optional

Preparing the way for the LPC device, which is just a plaform_device without
interrupts.

BUG=chromium:239197
BRANCH=all
TEST=none

We don't use this yet, so nothing to test.

Original-Change-Id: I0cbee48e1161aa525b400c1f5511ae6b7e657415
Signed-off-by: Bill Richardson <wfrichar@chromium.org>
Reviewed-on: https://gerrit-int.chromium.org/38361
Commit-Queue: Simon Glass <sjg@google.com>
Reviewed-by: Simon Glass <sjg@google.com>
Tested-by: Simon Glass <sjg@google.com>
(cherry picked from commit c4414f0944758730f4f14cb83ba50a44138f4fca)

Change-Id: Ie5cabe25fbbcdbfd59963cf081126a74fee6639d
Reviewed-on: https://gerrit.chromium.org/gerrit/57058
Commit-Queue: Bill Richardson <wfrichar@chromium.org>
Tested-by: Bill Richardson <wfrichar@chromium.org>
Reviewed-by: Simon Glass <sjg@chromium.org>

Conflicts:

	drivers/mfd/cros_ec.c
CHROMIUM: cros_ec: Allow static din/dout buffers with cros_ec_register()

The lower-level driver may want to provide its own buffers. If so, there's
no need to allocate new ones.

BUG=chromium:239197
BRANCH=all
TEST=none

We don't use this yet, so nothing to test.

Original-Change-Id: I2224aef34288cc5cbc79d6b2d95353e5540c148f
Signed-off-by: Bill Richardson <wfrichar@chromium.org>
Reviewed-on: https://gerrit-int.chromium.org/38362
Reviewed-by: Simon Glass <sjg@google.com>
Tested-by: Simon Glass <sjg@google.com>
Commit-Queue: Simon Glass <sjg@google.com>
(cherry picked from commit adf97098254a4bc1726c1f2cabef45b853783faa)

Change-Id: I5bf49e4dc0e08699caf41fff6b599479853c28fd
Reviewed-on: https://gerrit.chromium.org/gerrit/57059
Commit-Queue: Bill Richardson <wfrichar@chromium.org>
Tested-by: Bill Richardson <wfrichar@chromium.org>
Reviewed-by: Simon Glass <sjg@chromium.org>

Conflicts:

	drivers/mfd/cros_ec.c
CHROMIUM: cros_ec: Tweak struct cros_ec_device for clarity

The members of struct cros_ec_device were improperly commented, and
intermixed the private and public sections. This is just cleanup to make it
more obvious what goes with what.

BUG=chromium:239197
BRANCH=all
TEST=none

Original-Change-Id: I27055cc386327fed9ca969febae91a1c61ee3a86
Signed-off-by: Bill Richardson <wfrichar@chromium.org>
Reviewed-on: https://gerrit-int.chromium.org/38363
Commit-Queue: Simon Glass <sjg@google.com>
Reviewed-by: Simon Glass <sjg@google.com>
Tested-by: Simon Glass <sjg@google.com>
(cherry picked from commit 8f451bc1d72b48a8111e141bc5d82b72e8a2dcd2)

Change-Id: I9731b8d1404596a19777ea243c29eb6da375104c
Reviewed-on: https://gerrit.chromium.org/gerrit/57061
Commit-Queue: Bill Richardson <wfrichar@chromium.org>
Tested-by: Bill Richardson <wfrichar@chromium.org>
Reviewed-by: Simon Glass <sjg@chromium.org>

Conflicts:

	drivers/mfd/cros_ec_spi.c
	include/linux/mfd/cros_ec.h
CHROMIUM: cros_ec: Add Chrome OS EC userspace device interface

This replaces the ad-hoc and unsafe userspace access methods with a single
common interface.

BUG=chromium:239197
BRANCH=all
TEST=none

Nothing to test until it's enabled.

Original-Change-Id: Id97392261e2dbebb66cb82114f0d36eb7c8c2ea7
Signed-off-by: Bill Richardson <wfrichar@chromium.org>
Reviewed-on: https://gerrit-int.chromium.org/38364
Reviewed-by: Simon Glass <sjg@google.com>
Tested-by: Simon Glass <sjg@google.com>
Commit-Queue: Simon Glass <sjg@google.com>
(cherry picked from commit e9b34fd6125f46229f14b60db248433f21afa5ab)

Change-Id: Ia1aeca7ab5da6a3e5e02f206ab3bfbea918cec37
Reviewed-on: https://gerrit.chromium.org/gerrit/57063
Reviewed-by: Simon Glass <sjg@chromium.org>
Commit-Queue: Bill Richardson <wfrichar@chromium.org>
Tested-by: Bill Richardson <wfrichar@chromium.org>
CHROMIUM: cros_ec: Export cros_ec symbols for use by other modules

The cros_ec.c driver provides cros_ec_register() and cros_ec_remove(), but
they aren't exported for other modules to use. Sometimes this cause build
failures.

BUG=chromium:239197
BRANCH=none
TEST=none

Change-Id: I018c3f1c22e63cd758ca989e57bd43b63d785b41
Signed-off-by: Bill Richardson <wfrichar@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/57243
CHROMIUM: add vbc_ec implementation

This allows kernel to read/write vboot context from EC's nvram.

BUG=chrome-os-partner:15609
TEST=manual

1. Make sure kernel is set to use vbc ec and change directory
$ cat /proc/device-tree/firmware/chromeos/nonvolatile-context-storage
mkbp
$ cd /sys/bus/platform/drivers/cros-ec-vbc/cros-ec-vbc.4

2. Test read
$ test $(cat vboot_context) = $(mosys nvram vboot read) ; echo $?
0

3. Test write
$ echo 0123456789abcdef0123456789abcdef > vboot_context
$ test 0123456789abcdef0123456789abcdef = $(mosys nvram vboot read) ; echo $?
0

Change-Id: I860921403c09e8c6cfb04713b5c76a9fd9ffbe47
[sonnyrao: topic sync: split into platform and exynos]
[dianders: fixes for 3.8 and upstream ec]
[bleung: topic sync for 3.10 : split into platform and cros_ec]
Signed-off-by: Che-Liang Chiou <clchiou@chromium.org>
Signed-off-by: Doug Anderson <dianders@chromium.org>
Reviewed-by: Simon Glass <sjg@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/49246

(cherry picked from commit 50599885904c380ed6a72aa2ca51f01e742d35a1)
Signed-off-by: Benson Leung <bleung@chromium.org>
CHROMIUM: cros_ec: Add cros_ec_lpc driver for x86 devices

This adds the LPC interface to the Chrome OS EC. Like the I2C and SPI
drivers, this allows userspace access to the EC.

BUG=chromium:239197
BRANCH=none
TEST=manual

It doesn't do anything until it's enabled.

Change-Id: I4d7a30f819f03eb7312b4d295ec03b4f3f072798
Signed-off-by: Bill Richardson <wfrichar@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/57244
CHROMIUM: cros_ec: Only need one memread ioctl in the cros_ec_dev API

This simplifies the cros_ec_dev.h API by reducing the number of ioctls to
two. Since none of the removed ioctls were actually implemented, there is no
visible change.

BUG=chromium:239197
BRANCH=none
TEST=none

No functional change yet; just refactoring and cleaning up code.

Change-Id: Id3df1b90968521060d59d9ecda35f77877571619
Signed-off-by: Bill Richardson <wfrichar@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/57245
CHROMIUM: cros_ec: Add optional direct read function to interface

Some EC implementations provide a direct memory-mapped interface to
some read-only constants. This enables those implementations (LPC at the
moment) to take advantage of that through a dedicated function.

BUG=chromium:239197
BRANCH=none
TEST=manual

Nothing to test until it's enabled.

Change-Id: I1813529b10e7c70c8a751fdc7240772a29d97f44
Signed-off-by: Bill Richardson <wfrichar@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/57246
CHROMIUM: cros_ec: Read EC version on start-up

It's useful to be able to see this information. The ordering is:

- cros_ec_dev interface version
- EC's RO image name
- EC's RW image name
- Currently running EC image (read-only, read-write)

BUG=chromium:239197
BRANCH=all
TEST=on pit:
cat /dev/cros_ec
1.0.0
pit_no_version
pit_no_version
read-only

Change-Id: Iab523ece2c395186ee7a17fd205fb07c93599972
Signed-off-by: Simon Glass <sjg@chromium.org>
(cherry picked from commit 548f2c6c4128b4633158412be93b1d0e4bf46cb0)
Reviewed-on: https://gerrit.chromium.org/gerrit/58199
Reviewed-by: Randall Spangler <rspangler@chromium.org>
CHROMIUM: drivers: mfd: changed compare to >= of array size

smatch error:
drivers/mfd/cros_ec_dev.c:72 ec_get_version() error: buffer overflow 'current_image_name' 4 <= 4

Changed compare in ec_get_version of current_iomage_name array size from > to >=.

BUG=chromium:237705
TEST=FEATURES=test emerge-$B chromeos-kernel-next

Change-Id: I7c9037017fb945c693e740b77e6f116cd9841d16
Signed-off-by: Paul Taysom <taysom@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/59120
Reviewed-by: Sonny Rao <sonnyrao@chromium.org>
cros_ec: Allow multiple clients to open /dev/cros_ec

There is no particular reason to only allow a single client at a time,
and it is painful for clients to retry. So use locking to support
multiple clients.

BUG=chrome-os-partner:20388
TEST=manual
Run this in the factory image:
while true; do mosys nvram vboot read |grep -v 7000; done

See that we no longer get errors reported from mosys:

Error when probing GEC via devfs
Platform not supported

Change-Id: I6e2c70db6f8cb4e8399cea4e9e4fc904e42a5277
Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/59530
Reviewed-by: Bill Richardson <wfrichar@chromium.org>
CHROMIUM: Sync cros_ec_commands.h to latest EC version

This doesn't add any new functionality, it just makes sure that the kernel's
version of the EC commands matches the EC's.

BUG=chromium:241211
BRANCH=none
TEST=none

Change-Id: If6bfe9ad9f75b109c1995017c11ccd6bb137e2d2
Signed-off-by: Bill Richardson <wfrichar@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/51313
CHROMIUM: Sync to the latest cros_ec_commands.h from EC sources

This just updates include/linux/mfd/cros_ec_commands.h to match the latest
EC version (which is the One True Source for such things).

BUG=chromium:242706
BRANCH=falco,peppy
TEST=manual

Run "ectool version". It should continue to work.

Change-Id: Iefbee2dc04e7d0b316708964ac452e8bd35e4ca1
Signed-off-by: Bill Richardson <wfrichar@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/61792
Reviewed-by: Randall Spangler <rspangler@chromium.org>
Reviewed-by: Simon Glass <sjg@chromium.org>
mfd: Add mutex to cros_ec_spi

The main transfer function for cros_ec_spi can be called by more than
one client at a time.  Make sure that those clients don't stomp on
each other by locking the bus for the duration of the transfer
function.

BUG=chrome-os-partner:19249
TEST=Change the code to use "mutex_trylock" instead of "mutex_lock"
and print when the "try" failed.  Now use pydevi2c and loop with some
large transfers like:
  >>> tps = I2CDevice(20, 0x48, force=True)
  >>> while True: x = tps.Get(0, 230)
While doing this, bang on the keyboard.  See messages like:
  [  100.094000] mutex actually mattered!
  [  100.132138] mutex actually mattered!

Change-Id: Ie7820d1d25c7e61a8250c7a6403ce17c897e7e5b
Original-Change-Id:  I4910b4b4d22d83f3fe5e1e3f2298b3efe81e3aaa
Signed-off-by: Doug Anderson <dianders@chromium.org>
Tested-by: Doug Anderson <dianders@google.com>
Reviewed-by: David Hendricks <dhendrix@google.com>
Reviewed-by: Simon Glass <sjg@google.com>
Commit-Queue: Doug Anderson <dianders@google.com>
(cherry picked from commit 1802db9a61d4055bc5e0ffa28955d43c14e90832)
Reviewed-on: https://gerrit.chromium.org/gerrit/56516
Reviewed-by: Simon Glass <sjg@chromium.org>

Conflicts:

	drivers/mfd/cros_ec_spi.c
mfd: Make the cros_ec_spi timeout more reliable

The cros_ec_spi transfer had two problems with its timeout code:

1. It looked at the timeout even in the case that it found valid data.
2. If the cros_ec_spi code got switched out for a while, it's possible
   it could get a timeout after a single loop.  Let's be paranoid and
   make sure we do one last transfer after the timeout expires.

BUG=chrome-os-partner:19249
TEST=Use pydevi2c to stress with small transfers:
  >>> tps = I2CDevice(20, 0x48, force=True)
  >>> while True: x = tps.Get(0, 20)
...and get no errors

Change-Id: Ibbcb66c863d45fcfe3acd6bd9d9451060836fea0
Original-Change-Id:  I8ee980c6d79c4aea81eff442df5413b7ba367fa0
Signed-off-by: Doug Anderson <dianders@chromium.org>
Tested-by: Doug Anderson <dianders@google.com>
Reviewed-by: David Hendricks <dhendrix@google.com>
Reviewed-by: Simon Glass <sjg@google.com>
Commit-Queue: Doug Anderson <dianders@google.com>
(cherry picked from commit 03b12def71bb6b28d1c0adf80125b5e75364c74a)
Reviewed-on: https://gerrit.chromium.org/gerrit/56517
Reviewed-by: Simon Glass <sjg@chromium.org>

Conflicts:

	drivers/mfd/cros_ec_spi.c
mfd: Increase cros_ec_spi deadline from 5ms to 100ms

We're adding i2c tunneling to the list of things that goes over
cros_ec.  i2c tunneling can be slooooooow, so increase our deadline to
100ms to account for that.

BUG=chrome-os-partner:19249
TEST=Use pydevi2c to do large transfers:
  >>> tps = I2CDevice(20, 0x48, force=True)
  >>> while True: x = tps.Get(0, 230)
...and get no errors

Change-Id: I77a1058711a92c4471dd2df875cac52350370c1a
Original-Change-Id:  I700947890913b82b9d63279b6c998a2a10b009a7
Signed-off-by: Doug Anderson <dianders@chromium.org>
Tested-by: Doug Anderson <dianders@google.com>
Reviewed-by: David Hendricks <dhendrix@google.com>
Reviewed-by: Simon Glass <sjg@google.com>
Commit-Queue: Doug Anderson <dianders@google.com>
(cherry picked from commit 571dd35b479f551fdf15366add94075063c23db9)
Reviewed-on: https://gerrit.chromium.org/gerrit/56518
Reviewed-by: Simon Glass <sjg@chromium.org>
CHROMIUM: mfd: cros_ec: Init cros_ec_spi earlier

We'd like to move a few things earlier in the boot sequence so we can
get the LCD on a bit earlier.  That means:

* We need access to the FETs earlier
* We need tps65090 driver loaded earlier since it controls the FETs
* We need the i2c tunnel earlier since tps65090 is through the tunnel
* We need cros_ec earlier since i2c tunnel uses cros_ec

BUG=chromium:239659
TEST=With all patches together see that tps65090 probe happens much
earlier in the boot sequence.

Change-Id: I9bc235c7e28297f9693a167a7174799ff0f68fdf
Signed-off-by: Doug Anderson <dianders@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/57041
Commit-Queue: Simon Glass <sjg@chromium.org>
Reviewed-by: Simon Glass <sjg@chromium.org>