Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / gpu / drm / panel / panel-samsung-s6e63m0-spi.c
blob326deb3177b6c1472653903fa073f3b4f00278cb
1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/device.h>
4 #include <linux/module.h>
5 #include <linux/spi/spi.h>
6 #include <linux/delay.h>
8 #include <drm/drm_print.h>
10 #include "panel-samsung-s6e63m0.h"
12 #define DATA_MASK 0x100
14 static int s6e63m0_spi_dcs_read(struct device *dev, const u8 cmd, u8 *data)
16 struct spi_device *spi = to_spi_device(dev);
17 u16 buf[1];
18 u16 rbuf[1];
19 int ret;
21 /* SPI buffers are always in CPU order */
22 buf[0] = (u16)cmd;
23 ret = spi_write_then_read(spi, buf, 2, rbuf, 2);
24 dev_dbg(dev, "READ CMD: %04x RET: %04x\n", buf[0], rbuf[0]);
25 if (!ret)
26 /* These high 8 bits of the 9 contains the readout */
27 *data = (rbuf[0] & 0x1ff) >> 1;
29 return ret;
32 static int s6e63m0_spi_write_word(struct device *dev, u16 data)
34 struct spi_device *spi = to_spi_device(dev);
36 /* SPI buffers are always in CPU order */
37 return spi_write(spi, &data, 2);
40 static int s6e63m0_spi_dcs_write(struct device *dev, const u8 *data, size_t len)
42 int ret = 0;
44 dev_dbg(dev, "SPI writing dcs seq: %*ph\n", (int)len, data);
47 * This sends 9 bits with the first bit (bit 8) set to 0
48 * This indicates that this is a command. Anything after the
49 * command is data.
51 ret = s6e63m0_spi_write_word(dev, *data);
53 while (!ret && --len) {
54 ++data;
55 /* This sends 9 bits with the first bit (bit 8) set to 1 */
56 ret = s6e63m0_spi_write_word(dev, *data | DATA_MASK);
59 if (ret) {
60 dev_err(dev, "SPI error %d writing dcs seq: %*ph\n", ret,
61 (int)len, data);
64 usleep_range(300, 310);
66 return ret;
69 static int s6e63m0_spi_probe(struct spi_device *spi)
71 struct device *dev = &spi->dev;
72 int ret;
74 spi->bits_per_word = 9;
75 /* Preserve e.g. SPI_3WIRE setting */
76 spi->mode |= SPI_MODE_3;
77 ret = spi_setup(spi);
78 if (ret < 0) {
79 dev_err(dev, "spi setup failed.\n");
80 return ret;
82 return s6e63m0_probe(dev, s6e63m0_spi_dcs_read, s6e63m0_spi_dcs_write,
83 false);
86 static int s6e63m0_spi_remove(struct spi_device *spi)
88 return s6e63m0_remove(&spi->dev);
91 static const struct of_device_id s6e63m0_spi_of_match[] = {
92 { .compatible = "samsung,s6e63m0" },
93 { /* sentinel */ }
95 MODULE_DEVICE_TABLE(of, s6e63m0_spi_of_match);
97 static struct spi_driver s6e63m0_spi_driver = {
98 .probe = s6e63m0_spi_probe,
99 .remove = s6e63m0_spi_remove,
100 .driver = {
101 .name = "panel-samsung-s6e63m0",
102 .of_match_table = s6e63m0_spi_of_match,
105 module_spi_driver(s6e63m0_spi_driver);
107 MODULE_AUTHOR("Paweł Chmiel <pawel.mikolaj.chmiel@gmail.com>");
108 MODULE_DESCRIPTION("s6e63m0 LCD SPI Driver");
109 MODULE_LICENSE("GPL v2");