static int smb347_set_voltage_limits(struct smb347_charger *smb) { int ret, val; ret = smb347_read(smb, CFG_FLOAT_VOLTAGE); if (ret < 0) return ret; if (smb->pdata->pre_to_fast_voltage) { val = smb->pdata->pre_to_fast_voltage; val = clamp_val(val, 2400000, 3000000) - 2400000; val /= 200000; ret &= ~CFG_FLOAT_VOLTAGE_THRESHOLD_MASK; ret |= val << CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT; } if (smb->pdata->max_charge_voltage) { val = smb->pdata->max_charge_voltage; val = clamp_val(val, 3500000, 4500000) - 3500000; val /= 20000; ret |= val; } return smb347_write(smb, CFG_FLOAT_VOLTAGE, ret); }
static int mxr_try_format(struct mxr_device *mdev, struct v4l2_subdev_fh *fh, u32 pad, struct v4l2_mbus_framefmt *fmt, enum v4l2_subdev_format_whence which) { struct v4l2_mbus_framefmt mbus_fmt; fmt->width = clamp_val(fmt->width, 1, 32767); fmt->height = clamp_val(fmt->height, 1, 2047); switch (pad) { case MXR_PAD_SINK_GSCALER: fmt->code = V4L2_MBUS_FMT_YUV8_1X24; break; case MXR_PAD_SINK_GRP0: case MXR_PAD_SINK_GRP1: fmt->code = mxr_adjust_graph_format(fmt->code); break; case MXR_PAD_SOURCE_GSCALER: case MXR_PAD_SOURCE_GRP0: case MXR_PAD_SOURCE_GRP1: mxr_get_mbus_fmt(mdev, &mbus_fmt); fmt->code = (fmt->code == V4L2_MBUS_FMT_YUV8_1X24) ? V4L2_MBUS_FMT_YUV8_1X24 : V4L2_MBUS_FMT_XRGB8888_4X8_LE; fmt->width = mbus_fmt.width; fmt->height = mbus_fmt.height; break; } return 0; }
static inline int lut_temp_to_reg(struct lm63_data *data, long val) { val -= data->temp2_offset; if (data->lut_temp_highres) return DIV_ROUND_CLOSEST(clamp_val(val, 0, 127500), 500); else return DIV_ROUND_CLOSEST(clamp_val(val, 0, 127000), 1000); }
static inline u8 FAN_TO_REG(long rpm, int div) { long rpmdiv; if (rpm == 0) return 0; rpmdiv = clamp_val(rpm, 1, 960000) * div; return clamp_val((480000 + rpmdiv / 2) / rpmdiv, 1, 255); }
static u8 FAN_TO_REG(long rpm, int div) { if (rpm == -1) return 0; if (rpm == 0) return 255; rpm = clamp_val(rpm, 1, 1000000); return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254); }
void RectPrism::resize(XnPoint3D d) { this->width += d.X; this->height += d.Y; this->depth += d.Z; clamp_val(width, SIZE_MIN_DIM/2, SIZE_MAX_DIM/2); clamp_val(height, SIZE_MIN_DIM/2, SIZE_MAX_DIM/2); clamp_val(height, SIZE_MIN_DIM/2, SIZE_MAX_DIM/2); }
/* Return Both High byte and Low byte */ static u16 temp_to_reg(long temp, u8 config) { if (config & CFG_EXTEND) { temp = clamp_val(temp, -64000, 191000); temp += 64000; } else temp = clamp_val(temp, 0, 127000); return (temp * 160 + 312) / 625; /* Copied from TMP411 driver */ }
/* * Conversions. Limit checking is only done on the TO_REG * variants. * ******** VOLTAGE CONVERSIONS (Bob Dougherty) ******** * From HWMon.cpp (Copyright 1998-2000 Jonathan Teh Soon Yew): * voltagefactor[0]=1.25/2628; (2628/1.25=2102.4) // Vccp * voltagefactor[1]=1.25/2628; (2628/1.25=2102.4) // +2.5V * voltagefactor[2]=1.67/2628; (2628/1.67=1573.7) // +3.3V * voltagefactor[3]=2.6/2628; (2628/2.60=1010.8) // +5V * voltagefactor[4]=6.3/2628; (2628/6.30=417.14) // +12V * in[i]=(data[i+2]*25.0+133)*voltagefactor[i]; * That is: * volts = (25*regVal+133)*factor * regVal = (volts/factor-133)/25 * (These conversions were contributed by Jonathan Teh Soon Yew * <*****@*****.**>) */ static inline u8 IN_TO_REG(long val, int in_num) { if (in_num <= 1) return (u8) clamp_val((val * 21024 - 1205000) / 250000, 0, 255); else if (in_num == 2) return (u8) clamp_val((val * 15737 - 1205000) / 250000, 0, 255); else if (in_num == 3) return (u8) clamp_val((val * 10108 - 1205000) / 250000, 0, 255); else return (u8) clamp_val((val * 41714 - 12050000) / 2500000, 0, 255); }
static ssize_t applesmc_light_show(struct device *dev, struct device_attribute *attr, char *sysfsbuf) { static int data_length; int ret; u8 left = 0, right = 0; u8 buffer[10], query[6]; mutex_lock(&applesmc_lock); if (!data_length) { ret = applesmc_get_key_type(LIGHT_SENSOR_LEFT_KEY, query); if (ret) goto out; data_length = clamp_val(query[0], 0, 10); printk(KERN_INFO "applesmc: light sensor data length set to " "%d\n", data_length); } ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, data_length); /* newer macbooks report a single 10-bit bigendian value */ if (data_length == 10) { left = be16_to_cpu(*(__be16 *)(buffer + 6)) >> 2; goto out; }
static int adxl345_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int val, int val2, long mask) { struct adxl345_data *data = iio_priv(indio_dev); s64 n; switch (mask) { case IIO_CHAN_INFO_CALIBBIAS: /* * 8-bit resolution at +/- 2g, that is 4x accel data scale * factor */ return regmap_write(data->regmap, ADXL345_REG_OFS_AXIS(chan->address), val / 4); case IIO_CHAN_INFO_SAMP_FREQ: n = div_s64(val * NHZ_PER_HZ + val2, ADXL345_BASE_RATE_NANO_HZ); return regmap_update_bits(data->regmap, ADXL345_REG_BW_RATE, ADXL345_BW_RATE, clamp_val(ilog2(n), 0, ADXL345_BW_RATE)); } return -EINVAL; }
static ssize_t min_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { int ret; long temp; struct stts751_priv *priv = dev_get_drvdata(dev); if (kstrtol(buf, 10, &temp) < 0) return -EINVAL; mutex_lock(&priv->access_lock); /* HW works in range -64C to +127.937C */ temp = clamp_val(temp, -64000, priv->event_max); ret = stts751_set_temp_reg16(priv, temp, STTS751_REG_LLIM_H, STTS751_REG_LLIM_L); if (ret) goto exit; dev_dbg(&priv->client->dev, "setting event min %ld", temp); priv->event_min = temp; ret = count; exit: mutex_unlock(&priv->access_lock); return ret; }
static ssize_t set_pwm1(struct device *dev, struct device_attribute *devattr, const char *buf, size_t count) { struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); struct lm63_data *data = dev_get_drvdata(dev); struct i2c_client *client = data->client; int nr = attr->index; unsigned long val; int err; u8 reg; if (!(data->config_fan & 0x20)) /* register is read-only */ return -EPERM; err = kstrtoul(buf, 10, &val); if (err) return err; reg = nr ? LM63_REG_LUT_PWM(nr - 1) : LM63_REG_PWM_VALUE; val = clamp_val(val, 0, 255); mutex_lock(&data->update_lock); data->pwm1[nr] = data->pwm_highres ? val : (val * data->pwm1_freq * 2 + 127) / 255; i2c_smbus_write_byte_data(client, reg, data->pwm1[nr]); mutex_unlock(&data->update_lock); return count; }
static ssize_t set_crit_hyst(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { struct lm95245_data *data = dev_get_drvdata(dev); int index = to_sensor_dev_attr(attr)->index; struct i2c_client *client = data->client; unsigned long val; int hyst, limit; if (kstrtoul(buf, 10, &val) < 0) return -EINVAL; mutex_lock(&data->update_lock); limit = i2c_smbus_read_byte_data(client, lm95245_reg_address[index]); hyst = limit - val / 1000; hyst = clamp_val(hyst, 0, 31); data->regs[8] = hyst; /* shared crit hysteresis */ i2c_smbus_write_byte_data(client, LM95245_REG_RW_COMMON_HYSTERESIS, hyst); mutex_unlock(&data->update_lock); return count; }
static ssize_t set_limit(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { struct lm95245_data *data = dev_get_drvdata(dev); int index = to_sensor_dev_attr(attr)->index; struct i2c_client *client = data->client; unsigned long val; if (kstrtoul(buf, 10, &val) < 0) return -EINVAL; val /= 1000; val = clamp_val(val, 0, (index == 6 ? 127 : 255)); mutex_lock(&data->update_lock); data->valid = 0; i2c_smbus_write_byte_data(client, lm95245_reg_address[index], val); mutex_unlock(&data->update_lock); return count; }
static ssize_t set_temp_min(struct device *dev, struct device_attribute *devattr, const char *buf, size_t count) { int index = to_sensor_dev_attr(devattr)->index; struct max1668_data *data = dev_get_drvdata(dev); struct i2c_client *client = data->client; long temp; int ret; ret = kstrtol(buf, 10, &temp); if (ret < 0) return ret; mutex_lock(&data->update_lock); data->temp_min[index] = clamp_val(temp/1000, -128, 127); ret = i2c_smbus_write_byte_data(client, MAX1668_REG_LIML_WR(index), data->temp_min[index]); if (ret < 0) count = ret; mutex_unlock(&data->update_lock); return count; }
static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr, const char *buf, size_t count) { struct i2c_client *client = to_i2c_client(dev); struct max6650_data *data = i2c_get_clientdata(client); unsigned long pwm; int err; err = kstrtoul(buf, 10, &pwm); if (err) return err; pwm = clamp_val(pwm, 0, 255); mutex_lock(&data->update_lock); if (data->config & MAX6650_CFG_V12) data->dac = 180 - (180 * pwm)/255; else data->dac = 76 - (76 * pwm)/255; i2c_smbus_write_byte_data(client, MAX6650_REG_DAC, data->dac); mutex_unlock(&data->update_lock); return count; }
ShFraction<T>& ShFraction<T>::operator/=(const ShFraction& other) { LongType numerator = LongType(m_val) << BITS; LongType denom = LongType(other.m_val); m_val = clamp_val(numerator / denom); return (*this); }
static ssize_t therm_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { int ret; long temp; struct stts751_priv *priv = dev_get_drvdata(dev); if (kstrtol(buf, 10, &temp) < 0) return -EINVAL; /* HW works in range -64C to +127.937C */ temp = clamp_val(temp, -64000, 127937); mutex_lock(&priv->access_lock); ret = stts751_set_temp_reg8(priv, temp, STTS751_REG_TLIM); if (ret) goto exit; dev_dbg(&priv->client->dev, "setting therm %ld", temp); /* * hysteresis reg is relative to therm, so the HW does not need to be * adjusted, we need to update our local copy only. */ priv->hyst = temp - (priv->therm - priv->hyst); priv->therm = temp; exit: mutex_unlock(&priv->access_lock); if (ret) return ret; return count; }
static ssize_t set_fan(struct device *dev, struct device_attribute *devattr, const char *buf, size_t count) { struct emc6w201_data *data = dev_get_drvdata(dev); struct i2c_client *client = data->client; int sf = to_sensor_dev_attr_2(devattr)->index; int nr = to_sensor_dev_attr_2(devattr)->nr; int err; unsigned long val; err = kstrtoul(buf, 10, &val); if (err < 0) return err; if (val == 0) { val = 0xFFFF; } else { val = DIV_ROUND_CLOSEST(5400000U, val); val = clamp_val(val, 0, 0xFFFE); } mutex_lock(&data->update_lock); data->fan[sf][nr] = val; err = emc6w201_write16(client, EMC6W201_REG_FAN_MIN(nr), data->fan[sf][nr]); mutex_unlock(&data->update_lock); return err < 0 ? err : count; }
static void mxr_video_fix_geometry(struct mxr_layer *layer) { struct mxr_geometry *geo = &layer->geo; mxr_dbg(layer->mdev, "%s start\n", __func__); geo->dst.x_offset = clamp_val(geo->dst.x_offset, 0, geo->dst.full_width - 1); geo->dst.y_offset = clamp_val(geo->dst.y_offset, 0, geo->dst.full_height - 1); /* mixer scale-up is unuseful. so no use it */ geo->dst.width = clamp_val(geo->dst.width, 1, geo->dst.full_width - geo->dst.x_offset); geo->dst.height = clamp_val(geo->dst.height, 1, geo->dst.full_height - geo->dst.y_offset); }
static ssize_t set_offset(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { struct lm95234_data *data = dev_get_drvdata(dev); int index = to_sensor_dev_attr(attr)->index; int ret = lm95234_update_device(data); long val; if (ret) return ret; ret = kstrtol(buf, 10, &val); if (ret < 0) return ret; /* Accuracy is 1/2 degrees C */ val = clamp_val(DIV_ROUND_CLOSEST(val, 500), -128, 127); mutex_lock(&data->update_lock); data->toffset[index] = val; i2c_smbus_write_byte_data(data->client, LM95234_REG_OFFSET(index), val); mutex_unlock(&data->update_lock); return count; }
static ssize_t set_temp(struct device *dev, struct device_attribute *devattr, const char *buf, size_t count) { struct emc6w201_data *data = dev_get_drvdata(dev); struct i2c_client *client = data->client; int sf = to_sensor_dev_attr_2(devattr)->index; int nr = to_sensor_dev_attr_2(devattr)->nr; int err; long val; u8 reg; err = kstrtol(buf, 10, &val); if (err < 0) return err; val = DIV_ROUND_CLOSEST(val, 1000); reg = (sf == min) ? EMC6W201_REG_TEMP_LOW(nr) : EMC6W201_REG_TEMP_HIGH(nr); mutex_lock(&data->update_lock); data->temp[sf][nr] = clamp_val(val, -127, 127); err = emc6w201_write8(client, reg, data->temp[sf][nr]); mutex_unlock(&data->update_lock); return err < 0 ? err : count; }
static ssize_t set_tcrit1_hyst(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { struct lm95234_data *data = dev_get_drvdata(dev); int index = to_sensor_dev_attr(attr)->index; int ret = lm95234_update_device(data); long val; if (ret) return ret; ret = kstrtol(buf, 10, &val); if (ret < 0) return ret; val = DIV_ROUND_CLOSEST(val, 1000); val = clamp_val((int)data->tcrit1[index] - val, 0, 31); mutex_lock(&data->update_lock); data->thyst = val; i2c_smbus_write_byte_data(data->client, LM95234_REG_TCRIT_HYST, val); mutex_unlock(&data->update_lock); return count; }
static inline u16 rpm2tach(unsigned long rpm) { if (rpm == 0) return 0; return clamp_val((90000 * 60) / rpm, 1, 0xFFFF); }
static inline u8 FAN_TO_REG(long rpm, int div) { if (rpm <= 0) return 255; if (rpm > 1350000) return 1; return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254); }
static int mxr_try_crop(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh, unsigned int pad, struct v4l2_rect *r, enum v4l2_subdev_format_whence which) { struct v4l2_mbus_framefmt *fmt; fmt = __mxr_get_fmt(sd, fh, pad, which); if (fmt == NULL) return -EINVAL; r->left = clamp_val(r->left, 0, fmt->width); r->top = clamp_val(r->top, 0, fmt->height); r->width = clamp_val(r->width, 1, fmt->width - r->left); r->height = clamp_val(r->height, 1, fmt->height - r->top); return 0; }
static inline u16 temp2reg(struct adt7475_data *data, long val) { u16 ret; if (!(data->config5 & CONFIG5_TWOSCOMP)) { val = clamp_val(val, -64000, 191000); ret = (val + 64500) / 1000; } else { val = clamp_val(val, -128000, 127000); if (val < -500) ret = (256500 + val) / 1000; else ret = (val + 500) / 1000; } return ret << 2; }
static int max31790_write_fan(struct device *dev, u32 attr, int channel, long val) { struct max31790_data *data = dev_get_drvdata(dev); struct i2c_client *client = data->client; int target_count; int err = 0; u8 bits; int sr; mutex_lock(&data->update_lock); switch (attr) { case hwmon_fan_target: val = clamp_val(val, FAN_RPM_MIN, FAN_RPM_MAX); bits = bits_for_tach_period(val); data->fan_dynamics[channel] = ((data->fan_dynamics[channel] & ~MAX31790_FAN_DYN_SR_MASK) | (bits << MAX31790_FAN_DYN_SR_SHIFT)); err = i2c_smbus_write_byte_data(client, MAX31790_REG_FAN_DYNAMICS(channel), data->fan_dynamics[channel]); if (err < 0) break; sr = get_tach_period(data->fan_dynamics[channel]); target_count = RPM_TO_REG(val, sr); target_count = clamp_val(target_count, 0x1, 0x7FF); data->target_count[channel] = target_count << 5; err = i2c_smbus_write_word_swapped(client, MAX31790_REG_TARGET_COUNT(channel), data->target_count[channel]); break; default: err = -EOPNOTSUPP; break; } mutex_unlock(&data->update_lock); return err; }
static int rcar_gen3_thermal_set_trips(void *devdata, int low, int high) { struct rcar_gen3_thermal_tsc *tsc = devdata; low = clamp_val(low, -40000, 120000); high = clamp_val(high, -40000, 120000); rcar_gen3_thermal_write(tsc, REG_GEN3_IRQTEMP1, rcar_gen3_thermal_mcelsius_to_temp(tsc, low)); rcar_gen3_thermal_write(tsc, REG_GEN3_IRQTEMP2, rcar_gen3_thermal_mcelsius_to_temp(tsc, high)); tsc->low = low; tsc->high = high; return 0; }
static void report_synaptics_data(struct input_dev *input, const struct bcm5974_config *cfg, const struct tp_finger *f, int raw_n) { int abs_p = 0, abs_w = 0; if (raw_n) { int p = raw2int(f->touch_major); int w = raw2int(f->tool_major); if (p > 0 && raw2int(f->origin)) { abs_p = clamp_val(256 * p / cfg->p.max, 0, 255); abs_w = clamp_val(16 * w / cfg->w.max, 0, 15); } } input_report_abs(input, ABS_PRESSURE, abs_p); input_report_abs(input, ABS_TOOL_WIDTH, abs_w); }