Merge remote-tracking branches 'asoc/topic/rl6231', 'asoc/topic/rt5514' and 'asoc...
[sfrench/cifs-2.6.git] / drivers / mfd / cros_ec_spi.c
1 /*
2  * ChromeOS EC multi-function device (SPI)
3  *
4  * Copyright (C) 2012 Google, Inc
5  *
6  * This software is licensed under the terms of the GNU General Public
7  * License version 2, as published by the Free Software Foundation, and
8  * may be copied, distributed, and modified under those terms.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16 #include <linux/delay.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/mfd/cros_ec.h>
20 #include <linux/mfd/cros_ec_commands.h>
21 #include <linux/of.h>
22 #include <linux/platform_device.h>
23 #include <linux/slab.h>
24 #include <linux/spi/spi.h>
25
26
27 /* The header byte, which follows the preamble */
28 #define EC_MSG_HEADER                   0xec
29
30 /*
31  * Number of EC preamble bytes we read at a time. Since it takes
32  * about 400-500us for the EC to respond there is not a lot of
33  * point in tuning this. If the EC could respond faster then
34  * we could increase this so that might expect the preamble and
35  * message to occur in a single transaction. However, the maximum
36  * SPI transfer size is 256 bytes, so at 5MHz we need a response
37  * time of perhaps <320us (200 bytes / 1600 bits).
38  */
39 #define EC_MSG_PREAMBLE_COUNT           32
40
41 /*
42  * Allow for a long time for the EC to respond.  We support i2c
43  * tunneling and support fairly long messages for the tunnel (249
44  * bytes long at the moment).  If we're talking to a 100 kHz device
45  * on the other end and need to transfer ~256 bytes, then we need:
46  *  10 us/bit * ~10 bits/byte * ~256 bytes = ~25ms
47  *
48  * We'll wait 8 times that to handle clock stretching and other
49  * paranoia.  Note that some battery gas gauge ICs claim to have a
50  * clock stretch of 144ms in rare situations.  That's incentive for
51  * not directly passing i2c through, but it's too late for that for
52  * existing hardware.
53  *
54  * It's pretty unlikely that we'll really see a 249 byte tunnel in
55  * anything other than testing.  If this was more common we might
56  * consider having slow commands like this require a GET_STATUS
57  * wait loop.  The 'flash write' command would be another candidate
58  * for this, clocking in at 2-3ms.
59  */
60 #define EC_MSG_DEADLINE_MS              200
61
62 /*
63   * Time between raising the SPI chip select (for the end of a
64   * transaction) and dropping it again (for the next transaction).
65   * If we go too fast, the EC will miss the transaction. We know that we
66   * need at least 70 us with the 16 MHz STM32 EC, so go with 200 us to be
67   * safe.
68   */
69 #define EC_SPI_RECOVERY_TIME_NS (200 * 1000)
70
71 /**
72  * struct cros_ec_spi - information about a SPI-connected EC
73  *
74  * @spi: SPI device we are connected to
75  * @last_transfer_ns: time that we last finished a transfer, or 0 if there
76  *      if no record
77  * @start_of_msg_delay: used to set the delay_usecs on the spi_transfer that
78  *      is sent when we want to turn on CS at the start of a transaction.
79  * @end_of_msg_delay: used to set the delay_usecs on the spi_transfer that
80  *      is sent when we want to turn off CS at the end of a transaction.
81  */
82 struct cros_ec_spi {
83         struct spi_device *spi;
84         s64 last_transfer_ns;
85         unsigned int start_of_msg_delay;
86         unsigned int end_of_msg_delay;
87 };
88
89 static void debug_packet(struct device *dev, const char *name, u8 *ptr,
90                          int len)
91 {
92 #ifdef DEBUG
93         int i;
94
95         dev_dbg(dev, "%s: ", name);
96         for (i = 0; i < len; i++)
97                 pr_cont(" %02x", ptr[i]);
98
99         pr_cont("\n");
100 #endif
101 }
102
103 static int terminate_request(struct cros_ec_device *ec_dev)
104 {
105         struct cros_ec_spi *ec_spi = ec_dev->priv;
106         struct spi_message msg;
107         struct spi_transfer trans;
108         int ret;
109
110         /*
111          * Turn off CS, possibly adding a delay to ensure the rising edge
112          * doesn't come too soon after the end of the data.
113          */
114         spi_message_init(&msg);
115         memset(&trans, 0, sizeof(trans));
116         trans.delay_usecs = ec_spi->end_of_msg_delay;
117         spi_message_add_tail(&trans, &msg);
118
119         ret = spi_sync_locked(ec_spi->spi, &msg);
120
121         /* Reset end-of-response timer */
122         ec_spi->last_transfer_ns = ktime_get_ns();
123         if (ret < 0) {
124                 dev_err(ec_dev->dev,
125                         "cs-deassert spi transfer failed: %d\n",
126                         ret);
127         }
128
129         return ret;
130 }
131
132 /**
133  * receive_n_bytes - receive n bytes from the EC.
134  *
135  * Assumes buf is a pointer into the ec_dev->din buffer
136  */
137 static int receive_n_bytes(struct cros_ec_device *ec_dev, u8 *buf, int n)
138 {
139         struct cros_ec_spi *ec_spi = ec_dev->priv;
140         struct spi_transfer trans;
141         struct spi_message msg;
142         int ret;
143
144         BUG_ON(buf - ec_dev->din + n > ec_dev->din_size);
145
146         memset(&trans, 0, sizeof(trans));
147         trans.cs_change = 1;
148         trans.rx_buf = buf;
149         trans.len = n;
150
151         spi_message_init(&msg);
152         spi_message_add_tail(&trans, &msg);
153         ret = spi_sync_locked(ec_spi->spi, &msg);
154         if (ret < 0)
155                 dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret);
156
157         return ret;
158 }
159
160 /**
161  * cros_ec_spi_receive_packet - Receive a packet from the EC.
162  *
163  * This function has two phases: reading the preamble bytes (since if we read
164  * data from the EC before it is ready to send, we just get preamble) and
165  * reading the actual message.
166  *
167  * The received data is placed into ec_dev->din.
168  *
169  * @ec_dev: ChromeOS EC device
170  * @need_len: Number of message bytes we need to read
171  */
172 static int cros_ec_spi_receive_packet(struct cros_ec_device *ec_dev,
173                                       int need_len)
174 {
175         struct ec_host_response *response;
176         u8 *ptr, *end;
177         int ret;
178         unsigned long deadline;
179         int todo;
180
181         BUG_ON(ec_dev->din_size < EC_MSG_PREAMBLE_COUNT);
182
183         /* Receive data until we see the header byte */
184         deadline = jiffies + msecs_to_jiffies(EC_MSG_DEADLINE_MS);
185         while (true) {
186                 unsigned long start_jiffies = jiffies;
187
188                 ret = receive_n_bytes(ec_dev,
189                                       ec_dev->din,
190                                       EC_MSG_PREAMBLE_COUNT);
191                 if (ret < 0)
192                         return ret;
193
194                 ptr = ec_dev->din;
195                 for (end = ptr + EC_MSG_PREAMBLE_COUNT; ptr != end; ptr++) {
196                         if (*ptr == EC_SPI_FRAME_START) {
197                                 dev_dbg(ec_dev->dev, "msg found at %zd\n",
198                                         ptr - ec_dev->din);
199                                 break;
200                         }
201                 }
202                 if (ptr != end)
203                         break;
204
205                 /*
206                  * Use the time at the start of the loop as a timeout.  This
207                  * gives us one last shot at getting the transfer and is useful
208                  * in case we got context switched out for a while.
209                  */
210                 if (time_after(start_jiffies, deadline)) {
211                         dev_warn(ec_dev->dev, "EC failed to respond in time\n");
212                         return -ETIMEDOUT;
213                 }
214         }
215
216         /*
217          * ptr now points to the header byte. Copy any valid data to the
218          * start of our buffer
219          */
220         todo = end - ++ptr;
221         BUG_ON(todo < 0 || todo > ec_dev->din_size);
222         todo = min(todo, need_len);
223         memmove(ec_dev->din, ptr, todo);
224         ptr = ec_dev->din + todo;
225         dev_dbg(ec_dev->dev, "need %d, got %d bytes from preamble\n",
226                 need_len, todo);
227         need_len -= todo;
228
229         /* If the entire response struct wasn't read, get the rest of it. */
230         if (todo < sizeof(*response)) {
231                 ret = receive_n_bytes(ec_dev, ptr, sizeof(*response) - todo);
232                 if (ret < 0)
233                         return -EBADMSG;
234                 ptr += (sizeof(*response) - todo);
235                 todo = sizeof(*response);
236         }
237
238         response = (struct ec_host_response *)ec_dev->din;
239
240         /* Abort if data_len is too large. */
241         if (response->data_len > ec_dev->din_size)
242                 return -EMSGSIZE;
243
244         /* Receive data until we have it all */
245         while (need_len > 0) {
246                 /*
247                  * We can't support transfers larger than the SPI FIFO size
248                  * unless we have DMA. We don't have DMA on the ISP SPI ports
249                  * for Exynos. We need a way of asking SPI driver for
250                  * maximum-supported transfer size.
251                  */
252                 todo = min(need_len, 256);
253                 dev_dbg(ec_dev->dev, "loop, todo=%d, need_len=%d, ptr=%zd\n",
254                         todo, need_len, ptr - ec_dev->din);
255
256                 ret = receive_n_bytes(ec_dev, ptr, todo);
257                 if (ret < 0)
258                         return ret;
259
260                 ptr += todo;
261                 need_len -= todo;
262         }
263
264         dev_dbg(ec_dev->dev, "loop done, ptr=%zd\n", ptr - ec_dev->din);
265
266         return 0;
267 }
268
269 /**
270  * cros_ec_spi_receive_response - Receive a response from the EC.
271  *
272  * This function has two phases: reading the preamble bytes (since if we read
273  * data from the EC before it is ready to send, we just get preamble) and
274  * reading the actual message.
275  *
276  * The received data is placed into ec_dev->din.
277  *
278  * @ec_dev: ChromeOS EC device
279  * @need_len: Number of message bytes we need to read
280  */
281 static int cros_ec_spi_receive_response(struct cros_ec_device *ec_dev,
282                                         int need_len)
283 {
284         u8 *ptr, *end;
285         int ret;
286         unsigned long deadline;
287         int todo;
288
289         BUG_ON(ec_dev->din_size < EC_MSG_PREAMBLE_COUNT);
290
291         /* Receive data until we see the header byte */
292         deadline = jiffies + msecs_to_jiffies(EC_MSG_DEADLINE_MS);
293         while (true) {
294                 unsigned long start_jiffies = jiffies;
295
296                 ret = receive_n_bytes(ec_dev,
297                                       ec_dev->din,
298                                       EC_MSG_PREAMBLE_COUNT);
299                 if (ret < 0)
300                         return ret;
301
302                 ptr = ec_dev->din;
303                 for (end = ptr + EC_MSG_PREAMBLE_COUNT; ptr != end; ptr++) {
304                         if (*ptr == EC_SPI_FRAME_START) {
305                                 dev_dbg(ec_dev->dev, "msg found at %zd\n",
306                                         ptr - ec_dev->din);
307                                 break;
308                         }
309                 }
310                 if (ptr != end)
311                         break;
312
313                 /*
314                  * Use the time at the start of the loop as a timeout.  This
315                  * gives us one last shot at getting the transfer and is useful
316                  * in case we got context switched out for a while.
317                  */
318                 if (time_after(start_jiffies, deadline)) {
319                         dev_warn(ec_dev->dev, "EC failed to respond in time\n");
320                         return -ETIMEDOUT;
321                 }
322         }
323
324         /*
325          * ptr now points to the header byte. Copy any valid data to the
326          * start of our buffer
327          */
328         todo = end - ++ptr;
329         BUG_ON(todo < 0 || todo > ec_dev->din_size);
330         todo = min(todo, need_len);
331         memmove(ec_dev->din, ptr, todo);
332         ptr = ec_dev->din + todo;
333         dev_dbg(ec_dev->dev, "need %d, got %d bytes from preamble\n",
334                  need_len, todo);
335         need_len -= todo;
336
337         /* Receive data until we have it all */
338         while (need_len > 0) {
339                 /*
340                  * We can't support transfers larger than the SPI FIFO size
341                  * unless we have DMA. We don't have DMA on the ISP SPI ports
342                  * for Exynos. We need a way of asking SPI driver for
343                  * maximum-supported transfer size.
344                  */
345                 todo = min(need_len, 256);
346                 dev_dbg(ec_dev->dev, "loop, todo=%d, need_len=%d, ptr=%zd\n",
347                         todo, need_len, ptr - ec_dev->din);
348
349                 ret = receive_n_bytes(ec_dev, ptr, todo);
350                 if (ret < 0)
351                         return ret;
352
353                 debug_packet(ec_dev->dev, "interim", ptr, todo);
354                 ptr += todo;
355                 need_len -= todo;
356         }
357
358         dev_dbg(ec_dev->dev, "loop done, ptr=%zd\n", ptr - ec_dev->din);
359
360         return 0;
361 }
362
363 /**
364  * cros_ec_pkt_xfer_spi - Transfer a packet over SPI and receive the reply
365  *
366  * @ec_dev: ChromeOS EC device
367  * @ec_msg: Message to transfer
368  */
369 static int cros_ec_pkt_xfer_spi(struct cros_ec_device *ec_dev,
370                                 struct cros_ec_command *ec_msg)
371 {
372         struct ec_host_response *response;
373         struct cros_ec_spi *ec_spi = ec_dev->priv;
374         struct spi_transfer trans, trans_delay;
375         struct spi_message msg;
376         int i, len;
377         u8 *ptr;
378         u8 *rx_buf;
379         u8 sum;
380         u8 rx_byte;
381         int ret = 0, final_ret;
382
383         len = cros_ec_prepare_tx(ec_dev, ec_msg);
384         dev_dbg(ec_dev->dev, "prepared, len=%d\n", len);
385
386         /* If it's too soon to do another transaction, wait */
387         if (ec_spi->last_transfer_ns) {
388                 unsigned long delay;    /* The delay completed so far */
389
390                 delay = ktime_get_ns() - ec_spi->last_transfer_ns;
391                 if (delay < EC_SPI_RECOVERY_TIME_NS)
392                         ndelay(EC_SPI_RECOVERY_TIME_NS - delay);
393         }
394
395         rx_buf = kzalloc(len, GFP_KERNEL);
396         if (!rx_buf)
397                 return -ENOMEM;
398
399         spi_bus_lock(ec_spi->spi->master);
400
401         /*
402          * Leave a gap between CS assertion and clocking of data to allow the
403          * EC time to wakeup.
404          */
405         spi_message_init(&msg);
406         if (ec_spi->start_of_msg_delay) {
407                 memset(&trans_delay, 0, sizeof(trans_delay));
408                 trans_delay.delay_usecs = ec_spi->start_of_msg_delay;
409                 spi_message_add_tail(&trans_delay, &msg);
410         }
411
412         /* Transmit phase - send our message */
413         memset(&trans, 0, sizeof(trans));
414         trans.tx_buf = ec_dev->dout;
415         trans.rx_buf = rx_buf;
416         trans.len = len;
417         trans.cs_change = 1;
418         spi_message_add_tail(&trans, &msg);
419         ret = spi_sync_locked(ec_spi->spi, &msg);
420
421         /* Get the response */
422         if (!ret) {
423                 /* Verify that EC can process command */
424                 for (i = 0; i < len; i++) {
425                         rx_byte = rx_buf[i];
426                         if (rx_byte == EC_SPI_PAST_END  ||
427                             rx_byte == EC_SPI_RX_BAD_DATA ||
428                             rx_byte == EC_SPI_NOT_READY) {
429                                 ret = -EREMOTEIO;
430                                 break;
431                         }
432                 }
433         }
434
435         if (!ret)
436                 ret = cros_ec_spi_receive_packet(ec_dev,
437                                 ec_msg->insize + sizeof(*response));
438         else
439                 dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret);
440
441         final_ret = terminate_request(ec_dev);
442
443         spi_bus_unlock(ec_spi->spi->master);
444
445         if (!ret)
446                 ret = final_ret;
447         if (ret < 0)
448                 goto exit;
449
450         ptr = ec_dev->din;
451
452         /* check response error code */
453         response = (struct ec_host_response *)ptr;
454         ec_msg->result = response->result;
455
456         ret = cros_ec_check_result(ec_dev, ec_msg);
457         if (ret)
458                 goto exit;
459
460         len = response->data_len;
461         sum = 0;
462         if (len > ec_msg->insize) {
463                 dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)",
464                         len, ec_msg->insize);
465                 ret = -EMSGSIZE;
466                 goto exit;
467         }
468
469         for (i = 0; i < sizeof(*response); i++)
470                 sum += ptr[i];
471
472         /* copy response packet payload and compute checksum */
473         memcpy(ec_msg->data, ptr + sizeof(*response), len);
474         for (i = 0; i < len; i++)
475                 sum += ec_msg->data[i];
476
477         if (sum) {
478                 dev_err(ec_dev->dev,
479                         "bad packet checksum, calculated %x\n",
480                         sum);
481                 ret = -EBADMSG;
482                 goto exit;
483         }
484
485         ret = len;
486 exit:
487         kfree(rx_buf);
488         if (ec_msg->command == EC_CMD_REBOOT_EC)
489                 msleep(EC_REBOOT_DELAY_MS);
490
491         return ret;
492 }
493
494 /**
495  * cros_ec_cmd_xfer_spi - Transfer a message over SPI and receive the reply
496  *
497  * @ec_dev: ChromeOS EC device
498  * @ec_msg: Message to transfer
499  */
500 static int cros_ec_cmd_xfer_spi(struct cros_ec_device *ec_dev,
501                                 struct cros_ec_command *ec_msg)
502 {
503         struct cros_ec_spi *ec_spi = ec_dev->priv;
504         struct spi_transfer trans;
505         struct spi_message msg;
506         int i, len;
507         u8 *ptr;
508         u8 *rx_buf;
509         u8 rx_byte;
510         int sum;
511         int ret = 0, final_ret;
512
513         len = cros_ec_prepare_tx(ec_dev, ec_msg);
514         dev_dbg(ec_dev->dev, "prepared, len=%d\n", len);
515
516         /* If it's too soon to do another transaction, wait */
517         if (ec_spi->last_transfer_ns) {
518                 unsigned long delay;    /* The delay completed so far */
519
520                 delay = ktime_get_ns() - ec_spi->last_transfer_ns;
521                 if (delay < EC_SPI_RECOVERY_TIME_NS)
522                         ndelay(EC_SPI_RECOVERY_TIME_NS - delay);
523         }
524
525         rx_buf = kzalloc(len, GFP_KERNEL);
526         if (!rx_buf)
527                 return -ENOMEM;
528
529         spi_bus_lock(ec_spi->spi->master);
530
531         /* Transmit phase - send our message */
532         debug_packet(ec_dev->dev, "out", ec_dev->dout, len);
533         memset(&trans, 0, sizeof(trans));
534         trans.tx_buf = ec_dev->dout;
535         trans.rx_buf = rx_buf;
536         trans.len = len;
537         trans.cs_change = 1;
538         spi_message_init(&msg);
539         spi_message_add_tail(&trans, &msg);
540         ret = spi_sync_locked(ec_spi->spi, &msg);
541
542         /* Get the response */
543         if (!ret) {
544                 /* Verify that EC can process command */
545                 for (i = 0; i < len; i++) {
546                         rx_byte = rx_buf[i];
547                         if (rx_byte == EC_SPI_PAST_END  ||
548                             rx_byte == EC_SPI_RX_BAD_DATA ||
549                             rx_byte == EC_SPI_NOT_READY) {
550                                 ret = -EREMOTEIO;
551                                 break;
552                         }
553                 }
554         }
555
556         if (!ret)
557                 ret = cros_ec_spi_receive_response(ec_dev,
558                                 ec_msg->insize + EC_MSG_TX_PROTO_BYTES);
559         else
560                 dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret);
561
562         final_ret = terminate_request(ec_dev);
563
564         spi_bus_unlock(ec_spi->spi->master);
565
566         if (!ret)
567                 ret = final_ret;
568         if (ret < 0)
569                 goto exit;
570
571         ptr = ec_dev->din;
572
573         /* check response error code */
574         ec_msg->result = ptr[0];
575         ret = cros_ec_check_result(ec_dev, ec_msg);
576         if (ret)
577                 goto exit;
578
579         len = ptr[1];
580         sum = ptr[0] + ptr[1];
581         if (len > ec_msg->insize) {
582                 dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)",
583                         len, ec_msg->insize);
584                 ret = -ENOSPC;
585                 goto exit;
586         }
587
588         /* copy response packet payload and compute checksum */
589         for (i = 0; i < len; i++) {
590                 sum += ptr[i + 2];
591                 if (ec_msg->insize)
592                         ec_msg->data[i] = ptr[i + 2];
593         }
594         sum &= 0xff;
595
596         debug_packet(ec_dev->dev, "in", ptr, len + 3);
597
598         if (sum != ptr[len + 2]) {
599                 dev_err(ec_dev->dev,
600                         "bad packet checksum, expected %02x, got %02x\n",
601                         sum, ptr[len + 2]);
602                 ret = -EBADMSG;
603                 goto exit;
604         }
605
606         ret = len;
607 exit:
608         kfree(rx_buf);
609         if (ec_msg->command == EC_CMD_REBOOT_EC)
610                 msleep(EC_REBOOT_DELAY_MS);
611
612         return ret;
613 }
614
615 static void cros_ec_spi_dt_probe(struct cros_ec_spi *ec_spi, struct device *dev)
616 {
617         struct device_node *np = dev->of_node;
618         u32 val;
619         int ret;
620
621         ret = of_property_read_u32(np, "google,cros-ec-spi-pre-delay", &val);
622         if (!ret)
623                 ec_spi->start_of_msg_delay = val;
624
625         ret = of_property_read_u32(np, "google,cros-ec-spi-msg-delay", &val);
626         if (!ret)
627                 ec_spi->end_of_msg_delay = val;
628 }
629
630 static int cros_ec_spi_probe(struct spi_device *spi)
631 {
632         struct device *dev = &spi->dev;
633         struct cros_ec_device *ec_dev;
634         struct cros_ec_spi *ec_spi;
635         int err;
636
637         spi->bits_per_word = 8;
638         spi->mode = SPI_MODE_0;
639         err = spi_setup(spi);
640         if (err < 0)
641                 return err;
642
643         ec_spi = devm_kzalloc(dev, sizeof(*ec_spi), GFP_KERNEL);
644         if (ec_spi == NULL)
645                 return -ENOMEM;
646         ec_spi->spi = spi;
647         ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL);
648         if (!ec_dev)
649                 return -ENOMEM;
650
651         /* Check for any DT properties */
652         cros_ec_spi_dt_probe(ec_spi, dev);
653
654         spi_set_drvdata(spi, ec_dev);
655         ec_dev->dev = dev;
656         ec_dev->priv = ec_spi;
657         ec_dev->irq = spi->irq;
658         ec_dev->cmd_xfer = cros_ec_cmd_xfer_spi;
659         ec_dev->pkt_xfer = cros_ec_pkt_xfer_spi;
660         ec_dev->phys_name = dev_name(&ec_spi->spi->dev);
661         ec_dev->din_size = EC_MSG_PREAMBLE_COUNT +
662                            sizeof(struct ec_host_response) +
663                            sizeof(struct ec_response_get_protocol_info);
664         ec_dev->dout_size = sizeof(struct ec_host_request);
665
666         ec_spi->last_transfer_ns = ktime_get_ns();
667
668         err = cros_ec_register(ec_dev);
669         if (err) {
670                 dev_err(dev, "cannot register EC\n");
671                 return err;
672         }
673
674         device_init_wakeup(&spi->dev, true);
675
676         return 0;
677 }
678
679 static int cros_ec_spi_remove(struct spi_device *spi)
680 {
681         struct cros_ec_device *ec_dev;
682
683         ec_dev = spi_get_drvdata(spi);
684         cros_ec_remove(ec_dev);
685
686         return 0;
687 }
688
689 #ifdef CONFIG_PM_SLEEP
690 static int cros_ec_spi_suspend(struct device *dev)
691 {
692         struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
693
694         return cros_ec_suspend(ec_dev);
695 }
696
697 static int cros_ec_spi_resume(struct device *dev)
698 {
699         struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
700
701         return cros_ec_resume(ec_dev);
702 }
703 #endif
704
705 static SIMPLE_DEV_PM_OPS(cros_ec_spi_pm_ops, cros_ec_spi_suspend,
706                          cros_ec_spi_resume);
707
708 static const struct of_device_id cros_ec_spi_of_match[] = {
709         { .compatible = "google,cros-ec-spi", },
710         { /* sentinel */ },
711 };
712 MODULE_DEVICE_TABLE(of, cros_ec_spi_of_match);
713
714 static const struct spi_device_id cros_ec_spi_id[] = {
715         { "cros-ec-spi", 0 },
716         { }
717 };
718 MODULE_DEVICE_TABLE(spi, cros_ec_spi_id);
719
720 static struct spi_driver cros_ec_driver_spi = {
721         .driver = {
722                 .name   = "cros-ec-spi",
723                 .of_match_table = of_match_ptr(cros_ec_spi_of_match),
724                 .pm     = &cros_ec_spi_pm_ops,
725         },
726         .probe          = cros_ec_spi_probe,
727         .remove         = cros_ec_spi_remove,
728         .id_table       = cros_ec_spi_id,
729 };
730
731 module_spi_driver(cros_ec_driver_spi);
732
733 MODULE_LICENSE("GPL v2");
734 MODULE_DESCRIPTION("ChromeOS EC multi function device (SPI)");