1 /* 2 * STMicroelectronics hts221 spi driver 3 * 4 * Copyright 2016 STMicroelectronics Inc. 5 * 6 * Lorenzo Bianconi <lorenzo.bianconi@st.com> 7 * 8 * Licensed under the GPL-2. 9 */ 10 11 #include <linux/kernel.h> 12 #include <linux/module.h> 13 #include <linux/spi/spi.h> 14 #include <linux/slab.h> 15 #include "hts221.h" 16 17 #define SENSORS_SPI_READ 0x80 18 #define SPI_AUTO_INCREMENT 0x40 19 20 static int hts221_spi_read(struct device *dev, u8 addr, int len, u8 *data) 21 { 22 int err; 23 struct spi_device *spi = to_spi_device(dev); 24 struct iio_dev *iio_dev = spi_get_drvdata(spi); 25 struct hts221_hw *hw = iio_priv(iio_dev); 26 27 struct spi_transfer xfers[] = { 28 { 29 .tx_buf = hw->tb.tx_buf, 30 .bits_per_word = 8, 31 .len = 1, 32 }, 33 { 34 .rx_buf = hw->tb.rx_buf, 35 .bits_per_word = 8, 36 .len = len, 37 } 38 }; 39 40 if (len > 1) 41 addr |= SPI_AUTO_INCREMENT; 42 hw->tb.tx_buf[0] = addr | SENSORS_SPI_READ; 43 44 err = spi_sync_transfer(spi, xfers, ARRAY_SIZE(xfers)); 45 if (err < 0) 46 return err; 47 48 memcpy(data, hw->tb.rx_buf, len * sizeof(u8)); 49 50 return len; 51 } 52 53 static int hts221_spi_write(struct device *dev, u8 addr, int len, u8 *data) 54 { 55 struct spi_device *spi = to_spi_device(dev); 56 struct iio_dev *iio_dev = spi_get_drvdata(spi); 57 struct hts221_hw *hw = iio_priv(iio_dev); 58 59 struct spi_transfer xfers = { 60 .tx_buf = hw->tb.tx_buf, 61 .bits_per_word = 8, 62 .len = len + 1, 63 }; 64 65 if (len >= HTS221_TX_MAX_LENGTH) 66 return -ENOMEM; 67 68 if (len > 1) 69 addr |= SPI_AUTO_INCREMENT; 70 hw->tb.tx_buf[0] = addr; 71 memcpy(&hw->tb.tx_buf[1], data, len); 72 73 return spi_sync_transfer(spi, &xfers, 1); 74 } 75 76 static const struct hts221_transfer_function hts221_transfer_fn = { 77 .read = hts221_spi_read, 78 .write = hts221_spi_write, 79 }; 80 81 static int hts221_spi_probe(struct spi_device *spi) 82 { 83 struct hts221_hw *hw; 84 struct iio_dev *iio_dev; 85 86 iio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*hw)); 87 if (!iio_dev) 88 return -ENOMEM; 89 90 spi_set_drvdata(spi, iio_dev); 91 92 hw = iio_priv(iio_dev); 93 hw->name = spi->modalias; 94 hw->dev = &spi->dev; 95 hw->irq = spi->irq; 96 hw->tf = &hts221_transfer_fn; 97 98 return hts221_probe(iio_dev); 99 } 100 101 static const struct of_device_id hts221_spi_of_match[] = { 102 { .compatible = "st,hts221", }, 103 {}, 104 }; 105 MODULE_DEVICE_TABLE(of, hts221_spi_of_match); 106 107 static const struct spi_device_id hts221_spi_id_table[] = { 108 { HTS221_DEV_NAME }, 109 {}, 110 }; 111 MODULE_DEVICE_TABLE(spi, hts221_spi_id_table); 112 113 static struct spi_driver hts221_driver = { 114 .driver = { 115 .name = "hts221_spi", 116 .of_match_table = of_match_ptr(hts221_spi_of_match), 117 }, 118 .probe = hts221_spi_probe, 119 .id_table = hts221_spi_id_table, 120 }; 121 module_spi_driver(hts221_driver); 122 123 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi@st.com>"); 124 MODULE_DESCRIPTION("STMicroelectronics hts221 spi driver"); 125 MODULE_LICENSE("GPL v2"); 126