0001
0002
0003 #include <linux/backlight.h>
0004 #include <linux/delay.h>
0005 #include <linux/gpio/consumer.h>
0006 #include <linux/module.h>
0007 #include <linux/property.h>
0008 #include <linux/spi/spi.h>
0009
0010 #include <drm/drm_atomic_helper.h>
0011 #include <drm/drm_drv.h>
0012 #include <drm/drm_fb_helper.h>
0013 #include <drm/drm_gem_atomic_helper.h>
0014 #include <drm/drm_gem_cma_helper.h>
0015 #include <drm/drm_mipi_dbi.h>
0016 #include <drm/drm_modeset_helper.h>
0017
0018 #include <video/mipi_display.h>
0019
0020 #define ILI9163_FRMCTR1 0xb1
0021
0022 #define ILI9163_PWCTRL1 0xc0
0023 #define ILI9163_PWCTRL2 0xc1
0024 #define ILI9163_VMCTRL1 0xc5
0025 #define ILI9163_VMCTRL2 0xc7
0026 #define ILI9163_PWCTRLA 0xcb
0027 #define ILI9163_PWCTRLB 0xcf
0028
0029 #define ILI9163_EN3GAM 0xf2
0030
0031 #define ILI9163_MADCTL_BGR BIT(3)
0032 #define ILI9163_MADCTL_MV BIT(5)
0033 #define ILI9163_MADCTL_MX BIT(6)
0034 #define ILI9163_MADCTL_MY BIT(7)
0035
0036 static void yx240qv29_enable(struct drm_simple_display_pipe *pipe,
0037 struct drm_crtc_state *crtc_state,
0038 struct drm_plane_state *plane_state)
0039 {
0040 struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
0041 struct mipi_dbi *dbi = &dbidev->dbi;
0042 u8 addr_mode;
0043 int ret, idx;
0044
0045 if (!drm_dev_enter(pipe->crtc.dev, &idx))
0046 return;
0047
0048 DRM_DEBUG_KMS("\n");
0049
0050 ret = mipi_dbi_poweron_conditional_reset(dbidev);
0051 if (ret < 0)
0052 goto out_exit;
0053 if (ret == 1)
0054 goto out_enable;
0055
0056
0057 mipi_dbi_command(dbi, MIPI_DCS_SET_GAMMA_CURVE, 0x04);
0058 mipi_dbi_command(dbi, ILI9163_EN3GAM, 0x00);
0059
0060
0061 mipi_dbi_command(dbi, ILI9163_FRMCTR1, 0x0a, 0x14);
0062
0063
0064 mipi_dbi_command(dbi, ILI9163_PWCTRL1, 0x0a, 0x00);
0065 mipi_dbi_command(dbi, ILI9163_PWCTRL2, 0x02);
0066
0067
0068 mipi_dbi_command(dbi, ILI9163_VMCTRL1, 0x2f, 0x3e);
0069 mipi_dbi_command(dbi, ILI9163_VMCTRL2, 0x40);
0070
0071
0072 mipi_dbi_command(dbi, MIPI_DCS_SET_PIXEL_FORMAT, MIPI_DCS_PIXEL_FMT_16BIT);
0073
0074 mipi_dbi_command(dbi, MIPI_DCS_EXIT_SLEEP_MODE);
0075 msleep(100);
0076
0077 mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_ON);
0078 msleep(100);
0079
0080 out_enable:
0081 switch (dbidev->rotation) {
0082 default:
0083 addr_mode = ILI9163_MADCTL_MX | ILI9163_MADCTL_MY;
0084 break;
0085 case 90:
0086 addr_mode = ILI9163_MADCTL_MX | ILI9163_MADCTL_MV;
0087 break;
0088 case 180:
0089 addr_mode = 0;
0090 break;
0091 case 270:
0092 addr_mode = ILI9163_MADCTL_MY | ILI9163_MADCTL_MV;
0093 break;
0094 }
0095 addr_mode |= ILI9163_MADCTL_BGR;
0096 mipi_dbi_command(dbi, MIPI_DCS_SET_ADDRESS_MODE, addr_mode);
0097 mipi_dbi_enable_flush(dbidev, crtc_state, plane_state);
0098 out_exit:
0099 drm_dev_exit(idx);
0100 }
0101
0102 static const struct drm_simple_display_pipe_funcs ili9163_pipe_funcs = {
0103 .enable = yx240qv29_enable,
0104 .disable = mipi_dbi_pipe_disable,
0105 .update = mipi_dbi_pipe_update,
0106 .prepare_fb = drm_gem_simple_display_pipe_prepare_fb,
0107 };
0108
0109 static const struct drm_display_mode yx240qv29_mode = {
0110 DRM_SIMPLE_MODE(128, 160, 28, 35),
0111 };
0112
0113 DEFINE_DRM_GEM_CMA_FOPS(ili9163_fops);
0114
0115 static struct drm_driver ili9163_driver = {
0116 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
0117 .fops = &ili9163_fops,
0118 DRM_GEM_CMA_DRIVER_OPS_VMAP,
0119 .debugfs_init = mipi_dbi_debugfs_init,
0120 .name = "ili9163",
0121 .desc = "Ilitek ILI9163",
0122 .date = "20210208",
0123 .major = 1,
0124 .minor = 0,
0125 };
0126
0127 static const struct of_device_id ili9163_of_match[] = {
0128 { .compatible = "newhaven,1.8-128160EF" },
0129 { }
0130 };
0131 MODULE_DEVICE_TABLE(of, ili9163_of_match);
0132
0133 static const struct spi_device_id ili9163_id[] = {
0134 { "nhd-1.8-128160EF", 0 },
0135 { }
0136 };
0137 MODULE_DEVICE_TABLE(spi, ili9163_id);
0138
0139 static int ili9163_probe(struct spi_device *spi)
0140 {
0141 struct device *dev = &spi->dev;
0142 struct mipi_dbi_dev *dbidev;
0143 struct drm_device *drm;
0144 struct mipi_dbi *dbi;
0145 struct gpio_desc *dc;
0146 u32 rotation = 0;
0147 int ret;
0148
0149 dbidev = devm_drm_dev_alloc(dev, &ili9163_driver,
0150 struct mipi_dbi_dev, drm);
0151 if (IS_ERR(dbidev))
0152 return PTR_ERR(dbidev);
0153
0154 dbi = &dbidev->dbi;
0155 drm = &dbidev->drm;
0156
0157 spi_set_drvdata(spi, drm);
0158
0159 dbi->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
0160 if (IS_ERR(dbi->reset)) {
0161 DRM_DEV_ERROR(dev, "Failed to get gpio 'reset'\n");
0162 return PTR_ERR(dbi->reset);
0163 }
0164
0165 dc = devm_gpiod_get_optional(dev, "dc", GPIOD_OUT_LOW);
0166 if (IS_ERR(dc)) {
0167 DRM_DEV_ERROR(dev, "Failed to get gpio 'dc'\n");
0168 return PTR_ERR(dc);
0169 }
0170
0171 dbidev->backlight = devm_of_find_backlight(dev);
0172 if (IS_ERR(dbidev->backlight))
0173 return PTR_ERR(dbidev->backlight);
0174
0175 device_property_read_u32(dev, "rotation", &rotation);
0176
0177 ret = mipi_dbi_spi_init(spi, dbi, dc);
0178 if (ret)
0179 return ret;
0180
0181 ret = mipi_dbi_dev_init(dbidev, &ili9163_pipe_funcs, &yx240qv29_mode, rotation);
0182 if (ret)
0183 return ret;
0184
0185 drm_mode_config_reset(drm);
0186
0187 ret = drm_dev_register(drm, 0);
0188 if (ret)
0189 return ret;
0190
0191 drm_fbdev_generic_setup(drm, 0);
0192
0193 return 0;
0194 }
0195
0196 static void ili9163_remove(struct spi_device *spi)
0197 {
0198 struct drm_device *drm = spi_get_drvdata(spi);
0199
0200 drm_dev_unplug(drm);
0201 drm_atomic_helper_shutdown(drm);
0202 }
0203
0204 static void ili9163_shutdown(struct spi_device *spi)
0205 {
0206 drm_atomic_helper_shutdown(spi_get_drvdata(spi));
0207 }
0208
0209 static struct spi_driver ili9163_spi_driver = {
0210 .driver = {
0211 .name = "ili9163",
0212 .of_match_table = ili9163_of_match,
0213 },
0214 .id_table = ili9163_id,
0215 .probe = ili9163_probe,
0216 .remove = ili9163_remove,
0217 .shutdown = ili9163_shutdown,
0218 };
0219 module_spi_driver(ili9163_spi_driver);
0220
0221 MODULE_DESCRIPTION("Ilitek ILI9163 DRM driver");
0222 MODULE_AUTHOR("Daniel Mack <daniel@zonque.org>");
0223 MODULE_LICENSE("GPL");