Lines Matching refs:mcp
109 static int mcp_send_report(struct mcp2221 *mcp,
120 ret = hid_hw_output_report(mcp->hdev, buf, len);
133 static int mcp_send_data_req_status(struct mcp2221 *mcp,
139 reinit_completion(&mcp->wait_in_report);
141 ret = mcp_send_report(mcp, out_report, len);
145 t = wait_for_completion_timeout(&mcp->wait_in_report,
150 return mcp->status;
154 static int mcp_chk_last_cmd_status(struct mcp2221 *mcp)
156 memset(mcp->txbuf, 0, 8);
157 mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS;
159 return mcp_send_data_req_status(mcp, mcp->txbuf, 8);
163 static int mcp_cancel_last_cmd(struct mcp2221 *mcp)
165 memset(mcp->txbuf, 0, 8);
166 mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS;
167 mcp->txbuf[2] = MCP2221_I2C_CANCEL;
169 return mcp_send_data_req_status(mcp, mcp->txbuf, 8);
172 static int mcp_set_i2c_speed(struct mcp2221 *mcp)
176 memset(mcp->txbuf, 0, 8);
177 mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS;
178 mcp->txbuf[3] = MCP2221_I2C_SET_SPEED;
179 mcp->txbuf[4] = mcp->cur_i2c_clk_div;
181 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 8);
185 mcp_cancel_last_cmd(mcp);
199 static int mcp_i2c_write(struct mcp2221 *mcp,
212 mcp->txbuf[0] = type;
213 mcp->txbuf[1] = msg->len & 0xff;
214 mcp->txbuf[2] = msg->len >> 8;
215 mcp->txbuf[3] = (u8)(msg->addr << 1);
217 memcpy(&mcp->txbuf[4], &msg->buf[idx], len);
219 ret = mcp_send_data_req_status(mcp, mcp->txbuf, len + 4);
226 ret = mcp_chk_last_cmd_status(mcp);
257 static int mcp_i2c_smbus_read(struct mcp2221 *mcp,
264 mcp->txbuf[0] = type;
266 mcp->txbuf[1] = msg->len & 0xff;
267 mcp->txbuf[2] = msg->len >> 8;
268 mcp->txbuf[3] = (u8)(msg->addr << 1);
270 mcp->rxbuf = msg->buf;
272 mcp->txbuf[1] = smbus_len;
273 mcp->txbuf[2] = 0;
274 mcp->txbuf[3] = (u8)(smbus_addr << 1);
276 mcp->rxbuf = smbus_buf;
279 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 4);
283 mcp->rxbuf_idx = 0;
286 memset(mcp->txbuf, 0, 4);
287 mcp->txbuf[0] = MCP2221_I2C_GET_DATA;
289 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
293 ret = mcp_chk_last_cmd_status(mcp);
298 } while (mcp->rxbuf_idx < total_len);
307 struct mcp2221 *mcp = i2c_get_adapdata(adapter);
309 hid_hw_power(mcp->hdev, PM_HINT_FULLON);
311 mutex_lock(&mcp->lock);
314 ret = mcp_set_i2c_speed(mcp);
320 ret = mcp_i2c_smbus_read(mcp, msgs, MCP2221_I2C_RD_DATA,
323 ret = mcp_i2c_write(mcp, msgs, MCP2221_I2C_WR_DATA, 1);
334 ret = mcp_i2c_write(mcp, &msgs[0],
339 ret = mcp_i2c_smbus_read(mcp, &msgs[1],
357 hid_hw_power(mcp->hdev, PM_HINT_NORMAL);
358 mutex_unlock(&mcp->lock);
362 static int mcp_smbus_write(struct mcp2221 *mcp, u16 addr,
368 mcp->txbuf[0] = type;
369 mcp->txbuf[1] = len + 1; /* 1 is due to command byte itself */
370 mcp->txbuf[2] = 0;
371 mcp->txbuf[3] = (u8)(addr << 1);
372 mcp->txbuf[4] = command;
379 mcp->txbuf[5] = buf[0];
383 mcp->txbuf[5] = buf[0];
384 mcp->txbuf[6] = buf[1];
391 memcpy(&mcp->txbuf[5], buf, len);
395 ret = mcp_send_data_req_status(mcp, mcp->txbuf, data_len);
402 ret = mcp_chk_last_cmd_status(mcp);
416 struct mcp2221 *mcp = i2c_get_adapdata(adapter);
418 hid_hw_power(mcp->hdev, PM_HINT_FULLON);
420 mutex_lock(&mcp->lock);
422 ret = mcp_set_i2c_speed(mcp);
430 ret = mcp_i2c_smbus_read(mcp, NULL, MCP2221_I2C_RD_DATA,
433 ret = mcp_smbus_write(mcp, addr, command, NULL,
438 ret = mcp_i2c_smbus_read(mcp, NULL, MCP2221_I2C_RD_DATA,
441 ret = mcp_smbus_write(mcp, addr, command, NULL,
446 ret = mcp_smbus_write(mcp, addr, command, NULL,
451 ret = mcp_i2c_smbus_read(mcp, NULL,
455 ret = mcp_smbus_write(mcp, addr, command, &data->byte,
461 ret = mcp_smbus_write(mcp, addr, command, NULL,
466 ret = mcp_i2c_smbus_read(mcp, NULL,
470 ret = mcp_smbus_write(mcp, addr, command,
477 ret = mcp_smbus_write(mcp, addr, command, NULL,
482 mcp->rxbuf_idx = 0;
483 mcp->rxbuf = data->block;
484 mcp->txbuf[0] = MCP2221_I2C_GET_DATA;
485 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
493 ret = mcp_smbus_write(mcp, addr, command, data->block,
500 ret = mcp_smbus_write(mcp, addr, command, NULL,
505 mcp->rxbuf_idx = 0;
506 mcp->rxbuf = data->block;
507 mcp->txbuf[0] = MCP2221_I2C_GET_DATA;
508 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
516 ret = mcp_smbus_write(mcp, addr, command,
522 ret = mcp_smbus_write(mcp, addr, command,
528 ret = mcp_i2c_smbus_read(mcp, NULL,
533 ret = mcp_smbus_write(mcp, addr, command, data->block,
539 ret = mcp_i2c_smbus_read(mcp, NULL,
545 dev_err(&mcp->adapter.dev,
551 hid_hw_power(mcp->hdev, PM_HINT_NORMAL);
552 mutex_unlock(&mcp->lock);
574 struct mcp2221 *mcp = gpiochip_get_data(gc);
576 mcp->txbuf[0] = MCP2221_GPIO_GET;
578 mcp->gp_idx = offsetof(struct mcp_get_gpio, gpio[offset].value);
580 mutex_lock(&mcp->lock);
581 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
582 mutex_unlock(&mcp->lock);
590 struct mcp2221 *mcp = gpiochip_get_data(gc);
592 memset(mcp->txbuf, 0, 18);
593 mcp->txbuf[0] = MCP2221_GPIO_SET;
595 mcp->gp_idx = offsetof(struct mcp_set_gpio, gpio[offset].value);
597 mcp->txbuf[mcp->gp_idx - 1] = 1;
598 mcp->txbuf[mcp->gp_idx] = !!value;
600 mutex_lock(&mcp->lock);
601 mcp_send_data_req_status(mcp, mcp->txbuf, 18);
602 mutex_unlock(&mcp->lock);
605 static int mcp_gpio_dir_set(struct mcp2221 *mcp,
608 memset(mcp->txbuf, 0, 18);
609 mcp->txbuf[0] = MCP2221_GPIO_SET;
611 mcp->gp_idx = offsetof(struct mcp_set_gpio, gpio[offset].direction);
613 mcp->txbuf[mcp->gp_idx - 1] = 1;
614 mcp->txbuf[mcp->gp_idx] = val;
616 return mcp_send_data_req_status(mcp, mcp->txbuf, 18);
623 struct mcp2221 *mcp = gpiochip_get_data(gc);
625 mutex_lock(&mcp->lock);
626 ret = mcp_gpio_dir_set(mcp, offset, MCP2221_DIR_IN);
627 mutex_unlock(&mcp->lock);
636 struct mcp2221 *mcp = gpiochip_get_data(gc);
638 mutex_lock(&mcp->lock);
639 ret = mcp_gpio_dir_set(mcp, offset, MCP2221_DIR_OUT);
640 mutex_unlock(&mcp->lock);
655 struct mcp2221 *mcp = gpiochip_get_data(gc);
657 mcp->txbuf[0] = MCP2221_GPIO_GET;
659 mcp->gp_idx = offsetof(struct mcp_get_gpio, gpio[offset].direction);
661 mutex_lock(&mcp->lock);
662 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
663 mutex_unlock(&mcp->lock);
668 if (mcp->gpio_dir == MCP2221_DIR_IN)
675 static int mcp_get_i2c_eng_state(struct mcp2221 *mcp,
717 struct mcp2221 *mcp = hid_get_drvdata(hdev);
727 mcp->status = 0;
730 mcp->status = mcp_get_i2c_eng_state(mcp, data, 2);
732 complete(&mcp->wait_in_report);
738 if ((mcp->txbuf[3] == MCP2221_I2C_SET_SPEED) &&
740 mcp->status = -EAGAIN;
744 mcp->status = -ENXIO;
747 mcp->status = mcp_get_i2c_eng_state(mcp, data, 8);
750 mcp->status = -EIO;
752 complete(&mcp->wait_in_report);
759 mcp->status = -ENXIO;
762 if (!mcp_get_i2c_eng_state(mcp, data, 2)
764 mcp->status = 0;
768 mcp->status = -EIO;
772 buf = mcp->rxbuf;
773 memcpy(&buf[mcp->rxbuf_idx], &data[4], data[3]);
774 mcp->rxbuf_idx = mcp->rxbuf_idx + data[3];
775 mcp->status = 0;
778 mcp->status = -EIO;
781 mcp->status = -EIO;
783 complete(&mcp->wait_in_report);
789 if ((data[mcp->gp_idx] == MCP2221_ALT_F_NOT_GPIOV) ||
790 (data[mcp->gp_idx + 1] == MCP2221_ALT_F_NOT_GPIOD)) {
791 mcp->status = -ENOENT;
793 mcp->status = !!data[mcp->gp_idx];
794 mcp->gpio_dir = data[mcp->gp_idx + 1];
798 mcp->status = -EAGAIN;
800 complete(&mcp->wait_in_report);
806 if ((data[mcp->gp_idx] == MCP2221_ALT_F_NOT_GPIOV) ||
807 (data[mcp->gp_idx - 1] == MCP2221_ALT_F_NOT_GPIOV)) {
808 mcp->status = -ENOENT;
810 mcp->status = 0;
814 mcp->status = -EAGAIN;
816 complete(&mcp->wait_in_report);
820 mcp->status = -EIO;
821 complete(&mcp->wait_in_report);
831 struct mcp2221 *mcp;
833 mcp = devm_kzalloc(&hdev->dev, sizeof(*mcp), GFP_KERNEL);
834 if (!mcp)
862 mutex_init(&mcp->lock);
863 init_completion(&mcp->wait_in_report);
864 hid_set_drvdata(hdev, mcp);
865 mcp->hdev = hdev;
872 mcp->cur_i2c_clk_div = (12000000 / (i2c_clk_freq * 1000)) - 3;
874 mcp->adapter.owner = THIS_MODULE;
875 mcp->adapter.class = I2C_CLASS_HWMON;
876 mcp->adapter.algo = &mcp_i2c_algo;
877 mcp->adapter.retries = 1;
878 mcp->adapter.dev.parent = &hdev->dev;
879 snprintf(mcp->adapter.name, sizeof(mcp->adapter.name),
882 ret = i2c_add_adapter(&mcp->adapter);
887 i2c_set_adapdata(&mcp->adapter, mcp);
890 mcp->gc = devm_kzalloc(&hdev->dev, sizeof(*mcp->gc), GFP_KERNEL);
891 if (!mcp->gc) {
896 mcp->gc->label = "mcp2221_gpio";
897 mcp->gc->direction_input = mcp_gpio_direction_input;
898 mcp->gc->direction_output = mcp_gpio_direction_output;
899 mcp->gc->get_direction = mcp_gpio_get_direction;
900 mcp->gc->set = mcp_gpio_set;
901 mcp->gc->get = mcp_gpio_get;
902 mcp->gc->ngpio = MCP_NGPIO;
903 mcp->gc->base = -1;
904 mcp->gc->can_sleep = 1;
905 mcp->gc->parent = &hdev->dev;
907 ret = devm_gpiochip_add_data(&hdev->dev, mcp->gc, mcp);
914 i2c_del_adapter(&mcp->adapter);
916 hid_hw_close(mcp->hdev);
918 hid_hw_stop(mcp->hdev);
924 struct mcp2221 *mcp = hid_get_drvdata(hdev);
926 i2c_del_adapter(&mcp->adapter);
927 hid_hw_close(mcp->hdev);
928 hid_hw_stop(mcp->hdev);