^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * ST Microelectronics MFD: stmpe's spi client specific driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) ST Microelectronics SA 2011
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Author: Viresh Kumar <vireshk@kernel.org> for ST Microelectronics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/spi/spi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "stmpe.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define READ_CMD (1 << 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) static int spi_reg_read(struct stmpe *stmpe, u8 reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct spi_device *spi = stmpe->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) int status = spi_w8r16(spi, reg | READ_CMD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) return (status < 0) ? status : status >> 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static int spi_reg_write(struct stmpe *stmpe, u8 reg, u8 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct spi_device *spi = stmpe->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) u16 cmd = (val << 8) | reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return spi_write(spi, (const u8 *)&cmd, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static int spi_block_read(struct stmpe *stmpe, u8 reg, u8 length, u8 *values)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) int ret, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) for (i = 0; i < length; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) ret = spi_reg_read(stmpe, reg + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) *(values + i) = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static int spi_block_write(struct stmpe *stmpe, u8 reg, u8 length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) const u8 *values)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) int ret = 0, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) for (i = length; i > 0; i--, reg++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) ret = spi_reg_write(stmpe, reg, *(values + i - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static void spi_init(struct stmpe *stmpe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct spi_device *spi = stmpe->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) spi->bits_per_word = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) /* This register is only present for stmpe811 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (stmpe->variant->id_val == 0x0811)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) spi_reg_write(stmpe, STMPE811_REG_SPI_CFG, spi->mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (spi_setup(spi) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) dev_dbg(&spi->dev, "spi_setup failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) static struct stmpe_client_info spi_ci = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) .read_byte = spi_reg_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) .write_byte = spi_reg_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) .read_block = spi_block_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) .write_block = spi_block_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) .init = spi_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) stmpe_spi_probe(struct spi_device *spi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) const struct spi_device_id *id = spi_get_device_id(spi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) /* don't exceed max specified rate - 1MHz - Limitation of STMPE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (spi->max_speed_hz > 1000000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) (spi->max_speed_hz/1000));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) spi_ci.irq = spi->irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) spi_ci.client = spi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) spi_ci.dev = &spi->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return stmpe_probe(&spi_ci, id->driver_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static int stmpe_spi_remove(struct spi_device *spi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) struct stmpe *stmpe = spi_get_drvdata(spi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return stmpe_remove(stmpe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static const struct of_device_id stmpe_spi_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) { .compatible = "st,stmpe610", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) { .compatible = "st,stmpe801", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) { .compatible = "st,stmpe811", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) { .compatible = "st,stmpe1601", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) { .compatible = "st,stmpe2401", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) { .compatible = "st,stmpe2403", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) { /* sentinel */ },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) MODULE_DEVICE_TABLE(of, stmpe_spi_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static const struct spi_device_id stmpe_spi_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) { "stmpe610", STMPE610 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) { "stmpe801", STMPE801 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) { "stmpe811", STMPE811 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) { "stmpe1601", STMPE1601 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) { "stmpe2401", STMPE2401 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) { "stmpe2403", STMPE2403 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) MODULE_DEVICE_TABLE(spi, stmpe_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) static struct spi_driver stmpe_spi_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) .name = "stmpe-spi",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) .of_match_table = of_match_ptr(stmpe_spi_of_match),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) #ifdef CONFIG_PM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) .pm = &stmpe_dev_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) .probe = stmpe_spi_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) .remove = stmpe_spi_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) .id_table = stmpe_spi_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) static int __init stmpe_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) return spi_register_driver(&stmpe_spi_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) subsys_initcall(stmpe_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) static void __exit stmpe_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) spi_unregister_driver(&stmpe_spi_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) module_exit(stmpe_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) MODULE_DESCRIPTION("STMPE MFD SPI Interface Driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) MODULE_AUTHOR("Viresh Kumar <vireshk@kernel.org>");