Lines Matching refs:mcp

117 	struct mcp2221 *mcp;
127 static int mcp_send_report(struct mcp2221 *mcp,
138 ret = hid_hw_output_report(mcp->hdev, buf, len);
151 static int mcp_send_data_req_status(struct mcp2221 *mcp,
157 reinit_completion(&mcp->wait_in_report);
159 ret = mcp_send_report(mcp, out_report, len);
163 t = wait_for_completion_timeout(&mcp->wait_in_report,
168 return mcp->status;
172 static int mcp_chk_last_cmd_status(struct mcp2221 *mcp)
174 memset(mcp->txbuf, 0, 8);
175 mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS;
177 return mcp_send_data_req_status(mcp, mcp->txbuf, 8);
181 static int mcp_cancel_last_cmd(struct mcp2221 *mcp)
183 memset(mcp->txbuf, 0, 8);
184 mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS;
185 mcp->txbuf[2] = MCP2221_I2C_CANCEL;
187 return mcp_send_data_req_status(mcp, mcp->txbuf, 8);
190 static int mcp_set_i2c_speed(struct mcp2221 *mcp)
194 memset(mcp->txbuf, 0, 8);
195 mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS;
196 mcp->txbuf[3] = MCP2221_I2C_SET_SPEED;
197 mcp->txbuf[4] = mcp->cur_i2c_clk_div;
199 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 8);
203 mcp_cancel_last_cmd(mcp);
217 static int mcp_i2c_write(struct mcp2221 *mcp,
230 mcp->txbuf[0] = type;
231 mcp->txbuf[1] = msg->len & 0xff;
232 mcp->txbuf[2] = msg->len >> 8;
233 mcp->txbuf[3] = (u8)(msg->addr << 1);
235 memcpy(&mcp->txbuf[4], &msg->buf[idx], len);
237 ret = mcp_send_data_req_status(mcp, mcp->txbuf, len + 4);
244 ret = mcp_chk_last_cmd_status(mcp);
275 static int mcp_i2c_smbus_read(struct mcp2221 *mcp,
282 mcp->txbuf[0] = type;
284 mcp->txbuf[1] = msg->len & 0xff;
285 mcp->txbuf[2] = msg->len >> 8;
286 mcp->txbuf[3] = (u8)(msg->addr << 1);
288 mcp->rxbuf = msg->buf;
290 mcp->txbuf[1] = smbus_len;
291 mcp->txbuf[2] = 0;
292 mcp->txbuf[3] = (u8)(smbus_addr << 1);
294 mcp->rxbuf = smbus_buf;
297 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 4);
301 mcp->rxbuf_idx = 0;
304 memset(mcp->txbuf, 0, 4);
305 mcp->txbuf[0] = MCP2221_I2C_GET_DATA;
307 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
311 ret = mcp_chk_last_cmd_status(mcp);
316 } while (mcp->rxbuf_idx < total_len);
325 struct mcp2221 *mcp = i2c_get_adapdata(adapter);
327 hid_hw_power(mcp->hdev, PM_HINT_FULLON);
329 mutex_lock(&mcp->lock);
332 ret = mcp_set_i2c_speed(mcp);
338 ret = mcp_i2c_smbus_read(mcp, msgs, MCP2221_I2C_RD_DATA,
341 ret = mcp_i2c_write(mcp, msgs, MCP2221_I2C_WR_DATA, 1);
352 ret = mcp_i2c_write(mcp, &msgs[0],
357 ret = mcp_i2c_smbus_read(mcp, &msgs[1],
375 hid_hw_power(mcp->hdev, PM_HINT_NORMAL);
376 mutex_unlock(&mcp->lock);
380 static int mcp_smbus_write(struct mcp2221 *mcp, u16 addr,
386 mcp->txbuf[0] = type;
387 mcp->txbuf[1] = len + 1; /* 1 is due to command byte itself */
388 mcp->txbuf[2] = 0;
389 mcp->txbuf[3] = (u8)(addr << 1);
390 mcp->txbuf[4] = command;
397 mcp->txbuf[5] = buf[0];
401 mcp->txbuf[5] = buf[0];
402 mcp->txbuf[6] = buf[1];
409 memcpy(&mcp->txbuf[5], buf, len);
413 ret = mcp_send_data_req_status(mcp, mcp->txbuf, data_len);
420 ret = mcp_chk_last_cmd_status(mcp);
434 struct mcp2221 *mcp = i2c_get_adapdata(adapter);
436 hid_hw_power(mcp->hdev, PM_HINT_FULLON);
438 mutex_lock(&mcp->lock);
440 ret = mcp_set_i2c_speed(mcp);
448 ret = mcp_i2c_smbus_read(mcp, NULL, MCP2221_I2C_RD_DATA,
451 ret = mcp_smbus_write(mcp, addr, command, NULL,
456 ret = mcp_i2c_smbus_read(mcp, NULL, MCP2221_I2C_RD_DATA,
459 ret = mcp_smbus_write(mcp, addr, command, NULL,
464 ret = mcp_smbus_write(mcp, addr, command, NULL,
469 ret = mcp_i2c_smbus_read(mcp, NULL,
473 ret = mcp_smbus_write(mcp, addr, command, &data->byte,
479 ret = mcp_smbus_write(mcp, addr, command, NULL,
484 ret = mcp_i2c_smbus_read(mcp, NULL,
488 ret = mcp_smbus_write(mcp, addr, command,
495 ret = mcp_smbus_write(mcp, addr, command, NULL,
500 mcp->rxbuf_idx = 0;
501 mcp->rxbuf = data->block;
502 mcp->txbuf[0] = MCP2221_I2C_GET_DATA;
503 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
511 ret = mcp_smbus_write(mcp, addr, command, data->block,
518 ret = mcp_smbus_write(mcp, addr, command, NULL,
523 mcp->rxbuf_idx = 0;
524 mcp->rxbuf = data->block;
525 mcp->txbuf[0] = MCP2221_I2C_GET_DATA;
526 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
534 ret = mcp_smbus_write(mcp, addr, command,
540 ret = mcp_smbus_write(mcp, addr, command,
546 ret = mcp_i2c_smbus_read(mcp, NULL,
551 ret = mcp_smbus_write(mcp, addr, command, data->block,
557 ret = mcp_i2c_smbus_read(mcp, NULL,
563 dev_err(&mcp->adapter.dev,
569 hid_hw_power(mcp->hdev, PM_HINT_NORMAL);
570 mutex_unlock(&mcp->lock);
593 struct mcp2221 *mcp = gpiochip_get_data(gc);
595 mcp->txbuf[0] = MCP2221_GPIO_GET;
597 mcp->gp_idx = offsetof(struct mcp_get_gpio, gpio[offset]);
599 mutex_lock(&mcp->lock);
600 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
601 mutex_unlock(&mcp->lock);
609 struct mcp2221 *mcp = gpiochip_get_data(gc);
611 memset(mcp->txbuf, 0, 18);
612 mcp->txbuf[0] = MCP2221_GPIO_SET;
614 mcp->gp_idx = offsetof(struct mcp_set_gpio, gpio[offset].value);
616 mcp->txbuf[mcp->gp_idx - 1] = 1;
617 mcp->txbuf[mcp->gp_idx] = !!value;
619 mutex_lock(&mcp->lock);
620 mcp_send_data_req_status(mcp, mcp->txbuf, 18);
621 mutex_unlock(&mcp->lock);
624 static int mcp_gpio_dir_set(struct mcp2221 *mcp,
627 memset(mcp->txbuf, 0, 18);
628 mcp->txbuf[0] = MCP2221_GPIO_SET;
630 mcp->gp_idx = offsetof(struct mcp_set_gpio, gpio[offset].direction);
632 mcp->txbuf[mcp->gp_idx - 1] = 1;
633 mcp->txbuf[mcp->gp_idx] = val;
635 return mcp_send_data_req_status(mcp, mcp->txbuf, 18);
642 struct mcp2221 *mcp = gpiochip_get_data(gc);
644 mutex_lock(&mcp->lock);
645 ret = mcp_gpio_dir_set(mcp, offset, MCP2221_DIR_IN);
646 mutex_unlock(&mcp->lock);
655 struct mcp2221 *mcp = gpiochip_get_data(gc);
657 mutex_lock(&mcp->lock);
658 ret = mcp_gpio_dir_set(mcp, offset, MCP2221_DIR_OUT);
659 mutex_unlock(&mcp->lock);
674 struct mcp2221 *mcp = gpiochip_get_data(gc);
676 mcp->txbuf[0] = MCP2221_GPIO_GET;
678 mcp->gp_idx = offsetof(struct mcp_get_gpio, gpio[offset]);
680 mutex_lock(&mcp->lock);
681 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
682 mutex_unlock(&mcp->lock);
687 if (mcp->gpio_dir == MCP2221_DIR_IN)
695 static int mcp_get_i2c_eng_state(struct mcp2221 *mcp,
737 struct mcp2221 *mcp = hid_get_drvdata(hdev);
747 mcp->status = 0;
750 mcp->status = mcp_get_i2c_eng_state(mcp, data, 2);
752 complete(&mcp->wait_in_report);
758 if ((mcp->txbuf[3] == MCP2221_I2C_SET_SPEED) &&
760 mcp->status = -EAGAIN;
764 mcp->status = -ENXIO;
767 mcp->status = mcp_get_i2c_eng_state(mcp, data, 8);
769 memcpy(&mcp->adc_values, &data[50], sizeof(mcp->adc_values));
773 mcp->status = -EIO;
775 complete(&mcp->wait_in_report);
782 mcp->status = -ENXIO;
785 if (!mcp_get_i2c_eng_state(mcp, data, 2)
787 mcp->status = 0;
791 mcp->status = -EIO;
795 buf = mcp->rxbuf;
796 memcpy(&buf[mcp->rxbuf_idx], &data[4], data[3]);
797 mcp->rxbuf_idx = mcp->rxbuf_idx + data[3];
798 mcp->status = 0;
801 mcp->status = -EIO;
804 mcp->status = -EIO;
806 complete(&mcp->wait_in_report);
812 if ((data[mcp->gp_idx] == MCP2221_ALT_F_NOT_GPIOV) ||
813 (data[mcp->gp_idx + 1] == MCP2221_ALT_F_NOT_GPIOD)) {
814 mcp->status = -ENOENT;
816 mcp->status = !!data[mcp->gp_idx];
817 mcp->gpio_dir = data[mcp->gp_idx + 1];
821 mcp->status = -EAGAIN;
823 complete(&mcp->wait_in_report);
829 if ((data[mcp->gp_idx] == MCP2221_ALT_F_NOT_GPIOV) ||
830 (data[mcp->gp_idx - 1] == MCP2221_ALT_F_NOT_GPIOV)) {
831 mcp->status = -ENOENT;
833 mcp->status = 0;
837 mcp->status = -EAGAIN;
839 complete(&mcp->wait_in_report);
845 mcp->status = 0;
848 mcp->status = -EAGAIN;
850 complete(&mcp->wait_in_report);
856 memcpy(&mcp->mode, &data[22], 4);
858 mcp->dac_value = data[6] & GENMASK(4, 0);
860 mcp->status = 0;
863 mcp->status = -EAGAIN;
865 complete(&mcp->wait_in_report);
871 mcp->status = 0;
874 if (mcp->txbuf[1] != 0) {
875 mcp->status = -EIO;
885 mcp->dac_scale = tmp + 4;
887 mcp->dac_scale = 5;
892 mcp->adc_scale = tmp - 1;
894 mcp->adc_scale = 0;
900 mcp->status = -EAGAIN;
902 complete(&mcp->wait_in_report);
906 mcp->status = -EIO;
907 complete(&mcp->wait_in_report);
925 struct mcp2221 *mcp = hid_get_drvdata(hdev);
927 cancel_delayed_work_sync(&mcp->init_work);
936 struct mcp2221 *mcp = priv->mcp;
941 *val = 1 << mcp->dac_scale;
943 *val = 1 << mcp->adc_scale;
948 mutex_lock(&mcp->lock);
951 *val = mcp->dac_value;
955 ret = mcp_chk_last_cmd_status(mcp);
958 *val = le16_to_cpu((__force __le16) mcp->adc_values[channel->address]);
966 mutex_unlock(&mcp->lock);
976 struct mcp2221 *mcp = priv->mcp;
982 mutex_lock(&mcp->lock);
984 memset(mcp->txbuf, 0, 12);
985 mcp->txbuf[0] = MCP2221_SET_SRAM_SETTINGS;
986 mcp->txbuf[4] = BIT(7) | val;
988 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 12);
990 mcp->dac_value = val;
992 mutex_unlock(&mcp->lock);
1002 static int mcp_iio_channels(struct mcp2221 *mcp)
1009 struct iio_chan_spec *chan = &mcp->iio_channels[cnt];
1011 switch (mcp->mode[idx]) {
1042 struct mcp2221 *mcp = container_of(work, struct mcp2221, init_work.work);
1047 hid_hw_power(mcp->hdev, PM_HINT_FULLON);
1048 mutex_lock(&mcp->lock);
1050 mcp->txbuf[0] = MCP2221_GET_SRAM_SETTINGS;
1051 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
1056 num_channels = mcp_iio_channels(mcp);
1060 mcp->txbuf[0] = MCP2221_READ_FLASH_DATA;
1061 mcp->txbuf[1] = 0;
1062 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 2);
1067 indio_dev = devm_iio_device_alloc(&mcp->hdev->dev, sizeof(*data));
1072 data->mcp = mcp;
1077 indio_dev->channels = mcp->iio_channels;
1080 devm_iio_device_register(&mcp->hdev->dev, indio_dev);
1083 mutex_unlock(&mcp->lock);
1084 hid_hw_power(mcp->hdev, PM_HINT_NORMAL);
1089 mutex_unlock(&mcp->lock);
1090 hid_hw_power(mcp->hdev, PM_HINT_NORMAL);
1096 schedule_delayed_work(&mcp->init_work, msecs_to_jiffies(100));
1104 struct mcp2221 *mcp;
1106 mcp = devm_kzalloc(&hdev->dev, sizeof(*mcp), GFP_KERNEL);
1107 if (!mcp)
1136 mutex_init(&mcp->lock);
1137 init_completion(&mcp->wait_in_report);
1138 hid_set_drvdata(hdev, mcp);
1139 mcp->hdev = hdev;
1152 mcp->cur_i2c_clk_div = (12000000 / (i2c_clk_freq * 1000)) - 3;
1154 mcp->adapter.owner = THIS_MODULE;
1155 mcp->adapter.class = I2C_CLASS_HWMON;
1156 mcp->adapter.algo = &mcp_i2c_algo;
1157 mcp->adapter.retries = 1;
1158 mcp->adapter.dev.parent = &hdev->dev;
1159 snprintf(mcp->adapter.name, sizeof(mcp->adapter.name),
1162 i2c_set_adapdata(&mcp->adapter, mcp);
1163 ret = devm_i2c_add_adapter(&hdev->dev, &mcp->adapter);
1171 mcp->gc = devm_kzalloc(&hdev->dev, sizeof(*mcp->gc), GFP_KERNEL);
1172 if (!mcp->gc)
1175 mcp->gc->label = "mcp2221_gpio";
1176 mcp->gc->direction_input = mcp_gpio_direction_input;
1177 mcp->gc->direction_output = mcp_gpio_direction_output;
1178 mcp->gc->get_direction = mcp_gpio_get_direction;
1179 mcp->gc->set = mcp_gpio_set;
1180 mcp->gc->get = mcp_gpio_get;
1181 mcp->gc->ngpio = MCP_NGPIO;
1182 mcp->gc->base = -1;
1183 mcp->gc->can_sleep = 1;
1184 mcp->gc->parent = &hdev->dev;
1186 ret = devm_gpiochip_add_data(&hdev->dev, mcp->gc, mcp);
1192 INIT_DELAYED_WORK(&mcp->init_work, mcp_init_work);
1193 schedule_delayed_work(&mcp->init_work, msecs_to_jiffies(100));