1 /* 2 * STMicroelectronics hts221 sensor driver 3 * 4 * Copyright 2016 STMicroelectronics Inc. 5 * 6 * Lorenzo Bianconi <lorenzo.bianconi@st.com> 7 * 8 * Licensed under the GPL-2. 9 */ 10 #include <linux/kernel.h> 11 #include <linux/module.h> 12 #include <linux/device.h> 13 #include <linux/interrupt.h> 14 #include <linux/irqreturn.h> 15 16 #include <linux/iio/iio.h> 17 #include <linux/iio/trigger.h> 18 #include <linux/iio/events.h> 19 #include <linux/iio/trigger_consumer.h> 20 #include <linux/iio/triggered_buffer.h> 21 #include <linux/iio/buffer.h> 22 23 #include <linux/platform_data/st_sensors_pdata.h> 24 25 #include "hts221.h" 26 27 #define HTS221_REG_DRDY_HL_ADDR 0x22 28 #define HTS221_REG_DRDY_HL_MASK BIT(7) 29 #define HTS221_REG_DRDY_PP_OD_ADDR 0x22 30 #define HTS221_REG_DRDY_PP_OD_MASK BIT(6) 31 #define HTS221_REG_DRDY_EN_ADDR 0x22 32 #define HTS221_REG_DRDY_EN_MASK BIT(2) 33 #define HTS221_REG_STATUS_ADDR 0x27 34 #define HTS221_RH_DRDY_MASK BIT(1) 35 #define HTS221_TEMP_DRDY_MASK BIT(0) 36 37 static int hts221_trig_set_state(struct iio_trigger *trig, bool state) 38 { 39 struct iio_dev *iio_dev = iio_trigger_get_drvdata(trig); 40 struct hts221_hw *hw = iio_priv(iio_dev); 41 int err; 42 43 err = hts221_write_with_mask(hw, HTS221_REG_DRDY_EN_ADDR, 44 HTS221_REG_DRDY_EN_MASK, state); 45 46 return err < 0 ? err : 0; 47 } 48 49 static const struct iio_trigger_ops hts221_trigger_ops = { 50 .set_trigger_state = hts221_trig_set_state, 51 }; 52 53 static irqreturn_t hts221_trigger_handler_thread(int irq, void *private) 54 { 55 struct hts221_hw *hw = private; 56 u8 status; 57 int err; 58 59 err = hw->tf->read(hw->dev, HTS221_REG_STATUS_ADDR, sizeof(status), 60 &status); 61 if (err < 0) 62 return IRQ_HANDLED; 63 64 /* 65 * H_DA bit (humidity data available) is routed to DRDY line. 66 * Humidity sample is computed after temperature one. 67 * Here we can assume data channels are both available if H_DA bit 68 * is set in status register 69 */ 70 if (!(status & HTS221_RH_DRDY_MASK)) 71 return IRQ_NONE; 72 73 iio_trigger_poll_chained(hw->trig); 74 75 return IRQ_HANDLED; 76 } 77 78 int hts221_allocate_trigger(struct hts221_hw *hw) 79 { 80 struct iio_dev *iio_dev = iio_priv_to_dev(hw); 81 bool irq_active_low = false, open_drain = false; 82 struct device_node *np = hw->dev->of_node; 83 struct st_sensors_platform_data *pdata; 84 unsigned long irq_type; 85 int err; 86 87 irq_type = irqd_get_trigger_type(irq_get_irq_data(hw->irq)); 88 89 switch (irq_type) { 90 case IRQF_TRIGGER_HIGH: 91 case IRQF_TRIGGER_RISING: 92 break; 93 case IRQF_TRIGGER_LOW: 94 case IRQF_TRIGGER_FALLING: 95 irq_active_low = true; 96 break; 97 default: 98 dev_info(hw->dev, 99 "mode %lx unsupported, using IRQF_TRIGGER_RISING\n", 100 irq_type); 101 irq_type = IRQF_TRIGGER_RISING; 102 break; 103 } 104 105 err = hts221_write_with_mask(hw, HTS221_REG_DRDY_HL_ADDR, 106 HTS221_REG_DRDY_HL_MASK, irq_active_low); 107 if (err < 0) 108 return err; 109 110 pdata = (struct st_sensors_platform_data *)hw->dev->platform_data; 111 if ((np && of_property_read_bool(np, "drive-open-drain")) || 112 (pdata && pdata->open_drain)) { 113 irq_type |= IRQF_SHARED; 114 open_drain = true; 115 } 116 117 err = hts221_write_with_mask(hw, HTS221_REG_DRDY_PP_OD_ADDR, 118 HTS221_REG_DRDY_PP_OD_MASK, 119 open_drain); 120 if (err < 0) 121 return err; 122 123 err = devm_request_threaded_irq(hw->dev, hw->irq, NULL, 124 hts221_trigger_handler_thread, 125 irq_type | IRQF_ONESHOT, 126 hw->name, hw); 127 if (err) { 128 dev_err(hw->dev, "failed to request trigger irq %d\n", 129 hw->irq); 130 return err; 131 } 132 133 hw->trig = devm_iio_trigger_alloc(hw->dev, "%s-trigger", 134 iio_dev->name); 135 if (!hw->trig) 136 return -ENOMEM; 137 138 iio_trigger_set_drvdata(hw->trig, iio_dev); 139 hw->trig->ops = &hts221_trigger_ops; 140 hw->trig->dev.parent = hw->dev; 141 iio_dev->trig = iio_trigger_get(hw->trig); 142 143 return devm_iio_trigger_register(hw->dev, hw->trig); 144 } 145 146 static int hts221_buffer_preenable(struct iio_dev *iio_dev) 147 { 148 return hts221_set_enable(iio_priv(iio_dev), true); 149 } 150 151 static int hts221_buffer_postdisable(struct iio_dev *iio_dev) 152 { 153 return hts221_set_enable(iio_priv(iio_dev), false); 154 } 155 156 static const struct iio_buffer_setup_ops hts221_buffer_ops = { 157 .preenable = hts221_buffer_preenable, 158 .postenable = iio_triggered_buffer_postenable, 159 .predisable = iio_triggered_buffer_predisable, 160 .postdisable = hts221_buffer_postdisable, 161 }; 162 163 static irqreturn_t hts221_buffer_handler_thread(int irq, void *p) 164 { 165 u8 buffer[ALIGN(2 * HTS221_DATA_SIZE, sizeof(s64)) + sizeof(s64)]; 166 struct iio_poll_func *pf = p; 167 struct iio_dev *iio_dev = pf->indio_dev; 168 struct hts221_hw *hw = iio_priv(iio_dev); 169 struct iio_chan_spec const *ch; 170 int err; 171 172 /* humidity data */ 173 ch = &iio_dev->channels[HTS221_SENSOR_H]; 174 err = hw->tf->read(hw->dev, ch->address, HTS221_DATA_SIZE, 175 buffer); 176 if (err < 0) 177 goto out; 178 179 /* temperature data */ 180 ch = &iio_dev->channels[HTS221_SENSOR_T]; 181 err = hw->tf->read(hw->dev, ch->address, HTS221_DATA_SIZE, 182 buffer + HTS221_DATA_SIZE); 183 if (err < 0) 184 goto out; 185 186 iio_push_to_buffers_with_timestamp(iio_dev, buffer, 187 iio_get_time_ns(iio_dev)); 188 189 out: 190 iio_trigger_notify_done(hw->trig); 191 192 return IRQ_HANDLED; 193 } 194 195 int hts221_allocate_buffers(struct hts221_hw *hw) 196 { 197 return devm_iio_triggered_buffer_setup(hw->dev, iio_priv_to_dev(hw), 198 NULL, hts221_buffer_handler_thread, 199 &hts221_buffer_ops); 200 } 201 202 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi@st.com>"); 203 MODULE_DESCRIPTION("STMicroelectronics hts221 buffer driver"); 204 MODULE_LICENSE("GPL v2"); 205