static ssize_t show_linear(struct device *dev, struct device_attribute *attr, char *buf) { struct lm3533_bl *bl = dev_get_drvdata(dev); u8 val; u8 mask; int linear; int ret; int i = 0; for(i=0x10; i<0xB3; i++) { ret = lm3533_read(bl->lm3533, i, &val); if(ret) { printk("in %s,can not read [0x%02x]\n",__func__,i); } else { printk("in %s,read [0x%02x]: 0x%02x\n",__func__,i,val); } } ret = lm3533_read(bl->lm3533, LM3533_REG_CTRLBANK_AB_BCONF, &val); if (ret) return ret; mask = 1 << (2 * lm3533_bl_get_ctrlbank_id(bl) + 1); if (val & mask) linear = 1; else linear = 0; return scnprintf(buf, PAGE_SIZE, "%x\n", linear); }
static int _lm3533_als_get_zone(struct iio_dev *indio_dev, u8 *zone) { struct lm3533_als *als = iio_priv(indio_dev); u8 val; int ret; ret = lm3533_read(als->lm3533, LM3533_REG_ALS_ZONE_INFO, &val); if (ret) { dev_err(&indio_dev->dev, "failed to read zone\n"); return ret; } val = (val & LM3533_ALS_ZONE_MASK) >> LM3533_ALS_ZONE_SHIFT; *zone = min_t(u8, val, LM3533_ALS_ZONE_MAX); return 0; }
static int lm3533_als_get_threshold(struct iio_dev *indio_dev, unsigned nr, bool raising, u8 *val) { struct lm3533_als *als = iio_priv(indio_dev); u8 reg; int ret; if (nr > LM3533_ALS_THRESH_MAX) return -EINVAL; reg = lm3533_als_get_threshold_reg(nr, raising); ret = lm3533_read(als->lm3533, reg, val); if (ret) dev_err(&indio_dev->dev, "failed to get threshold\n"); return ret; }
static int lm3533_als_get_int_mode(struct iio_dev *indio_dev, int *enable) { struct lm3533_als *als = iio_priv(indio_dev); u8 mask = LM3533_ALS_INT_ENABLE_MASK; u8 val; int ret; ret = lm3533_read(als->lm3533, LM3533_REG_ALS_ZONE_INFO, &val); if (ret) { dev_err(&indio_dev->dev, "failed to get int mode\n"); return ret; } *enable = !!(val & mask); return 0; }
static ssize_t show_als_en(struct device *dev, struct device_attribute *attr, char *buf) { struct lm3533_bl *bl = dev_get_drvdata(dev); int ctrlbank = lm3533_bl_get_ctrlbank_id(bl); u8 val; u8 mask; bool enable; int ret; ret = lm3533_read(bl->lm3533, LM3533_REG_CTRLBANK_AB_BCONF, &val); if (ret) return ret; mask = 1 << (2 * ctrlbank); enable = val & mask; return scnprintf(buf, PAGE_SIZE, "%d\n", enable); }
static int lm3533_als_get_target(struct iio_dev *indio_dev, unsigned channel, unsigned zone, u8 *val) { struct lm3533_als *als = iio_priv(indio_dev); u8 reg; int ret; if (channel > LM3533_ALS_CHANNEL_CURRENT_MAX) return -EINVAL; if (zone > LM3533_ALS_ZONE_MAX) return -EINVAL; reg = lm3533_als_get_target_reg(channel, zone); ret = lm3533_read(als->lm3533, reg, val); if (ret) dev_err(&indio_dev->dev, "failed to get target current\n"); return ret; }
static int lm3533_als_set_threshold(struct iio_dev *indio_dev, unsigned nr, bool raising, u8 val) { struct lm3533_als *als = iio_priv(indio_dev); u8 val2; u8 reg, reg2; int ret; if (nr > LM3533_ALS_THRESH_MAX) return -EINVAL; reg = lm3533_als_get_threshold_reg(nr, raising); reg2 = lm3533_als_get_threshold_reg(nr, !raising); mutex_lock(&als->thresh_mutex); ret = lm3533_read(als->lm3533, reg2, &val2); if (ret) { dev_err(&indio_dev->dev, "failed to get threshold\n"); goto out; } /* * This device does not allow negative hysteresis (in fact, it uses * whichever value is smaller as the lower bound) so we need to make * sure that thresh_falling <= thresh_raising. */ if ((raising && (val < val2)) || (!raising && (val > val2))) { ret = -EINVAL; goto out; } ret = lm3533_write(als->lm3533, reg, val); if (ret) { dev_err(&indio_dev->dev, "failed to set threshold\n"); goto out; } out: mutex_unlock(&als->thresh_mutex); return ret; }
static ssize_t show_linear(struct device *dev, struct device_attribute *attr, char *buf) { struct lm3533_bl *bl = dev_get_drvdata(dev); u8 val; u8 mask; int linear; int ret; ret = lm3533_read(bl->lm3533, LM3533_REG_CTRLBANK_AB_BCONF, &val); if (ret) return ret; mask = 1 << (2 * lm3533_bl_get_ctrlbank_id(bl) + 1); if (val & mask) linear = 1; else linear = 0; return scnprintf(buf, PAGE_SIZE, "%x\n", linear); }
static int lm3533_als_get_adc(struct iio_dev *indio_dev, bool average, int *adc) { struct lm3533_als *als = iio_priv(indio_dev); u8 reg; u8 val; int ret; if (average) reg = LM3533_REG_ALS_READ_ADC_AVERAGE; else reg = LM3533_REG_ALS_READ_ADC_RAW; ret = lm3533_read(als->lm3533, reg, &val); if (ret) { dev_err(&indio_dev->dev, "failed to read adc\n"); return ret; } *adc = val; return 0; }