xref: /linux/drivers/gpu/drm/panel/panel-samsung-s6e63m0-spi.c (revision 02680c23d7b3febe45ea3d4f9818c2b2dc89020a)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/device.h>
4 #include <linux/module.h>
5 #include <linux/spi/spi.h>
6 #include <linux/delay.h>
7 
8 #include <drm/drm_print.h>
9 
10 #include "panel-samsung-s6e63m0.h"
11 
12 #define DATA_MASK	0x100
13 
14 static int s6e63m0_spi_dcs_read(struct device *dev, const u8 cmd, u8 *data)
15 {
16 	struct spi_device *spi = to_spi_device(dev);
17 	u16 buf[1];
18 	u16 rbuf[1];
19 	int ret;
20 
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;
28 
29 	return ret;
30 }
31 
32 static int s6e63m0_spi_write_word(struct device *dev, u16 data)
33 {
34 	struct spi_device *spi = to_spi_device(dev);
35 
36 	/* SPI buffers are always in CPU order */
37 	return spi_write(spi, &data, 2);
38 }
39 
40 static int s6e63m0_spi_dcs_write(struct device *dev, const u8 *data, size_t len)
41 {
42 	int ret = 0;
43 
44 	dev_dbg(dev, "SPI writing dcs seq: %*ph\n", (int)len, data);
45 
46 	/*
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.
50 	 */
51 	ret = s6e63m0_spi_write_word(dev, *data);
52 
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);
57 	}
58 
59 	if (ret) {
60 		dev_err(dev, "SPI error %d writing dcs seq: %*ph\n", ret,
61 			(int)len, data);
62 	}
63 
64 	usleep_range(300, 310);
65 
66 	return ret;
67 }
68 
69 static int s6e63m0_spi_probe(struct spi_device *spi)
70 {
71 	struct device *dev = &spi->dev;
72 	int ret;
73 
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;
81 	}
82 	return s6e63m0_probe(dev, s6e63m0_spi_dcs_read, s6e63m0_spi_dcs_write,
83 			     false);
84 }
85 
86 static int s6e63m0_spi_remove(struct spi_device *spi)
87 {
88 	return s6e63m0_remove(&spi->dev);
89 }
90 
91 static const struct of_device_id s6e63m0_spi_of_match[] = {
92 	{ .compatible = "samsung,s6e63m0" },
93 	{ /* sentinel */ }
94 };
95 MODULE_DEVICE_TABLE(of, s6e63m0_spi_of_match);
96 
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,
103 	},
104 };
105 module_spi_driver(s6e63m0_spi_driver);
106 
107 MODULE_AUTHOR("Paweł Chmiel <pawel.mikolaj.chmiel@gmail.com>");
108 MODULE_DESCRIPTION("s6e63m0 LCD SPI Driver");
109 MODULE_LICENSE("GPL v2");
110