Lines Matching +full:i2c +full:- +full:transfer +full:- +full:timeout +full:- +full:us
1 // SPDX-License-Identifier: GPL-2.0-only
11 * - maybe add timeout to commands ?
12 * - blocking version of time functions
13 * - polling version of i2c commands (including timer that works with
15 * - maybe avoid some data copies with i2c by directly using the smu cmd
17 * - understand SMU -> CPU events and implement reception of them via
91 struct smu_i2c_cmd *cmd_i2c_cur; /* pending i2c command */
116 if (list_empty(&smu->cmd_list))
120 cmd = list_entry(smu->cmd_list.next, struct smu_cmd, link);
121 smu->cmd_cur = cmd;
122 list_del(&cmd->link);
124 DPRINTK("SMU: starting cmd %x, %d bytes data\n", cmd->cmd,
125 cmd->data_len);
126 DPRINTK("SMU: data buffer: %8ph\n", cmd->data_buf);
129 smu->cmd_buf->cmd = cmd->cmd;
130 smu->cmd_buf->length = cmd->data_len;
131 memcpy(smu->cmd_buf->data, cmd->data_buf, cmd->data_len);
134 faddr = (unsigned long)smu->cmd_buf;
135 fend = faddr + smu->cmd_buf->length + 2;
145 * from memory. This might be related to the strange i2c based
148 if (smu->broken_nap)
152 * the SMU is actually communicating with us via i2c to the
155 writel(smu->cmd_buf_abs, smu->db_buf);
158 pmac_do_feature_call(PMAC_FTR_WRITE_GPIO, NULL, smu->doorbell, 4);
174 spin_lock_irqsave(&smu->lock, flags);
176 gpio = pmac_do_feature_call(PMAC_FTR_READ_GPIO, NULL, smu->doorbell);
178 spin_unlock_irqrestore(&smu->lock, flags);
182 cmd = smu->cmd_cur;
183 smu->cmd_cur = NULL;
197 faddr = (unsigned long)smu->cmd_buf;
201 ack = (~cmd->cmd) & 0xff;
202 if (ack != smu->cmd_buf->cmd) {
204 ack, smu->cmd_buf->cmd);
205 rc = -EIO;
207 reply_len = rc == 0 ? smu->cmd_buf->length : 0;
209 if (reply_len > cmd->reply_len) {
212 reply_len, cmd->reply_len);
213 reply_len = cmd->reply_len;
215 cmd->reply_len = reply_len;
216 if (cmd->reply_buf && reply_len)
217 memcpy(cmd->reply_buf, smu->cmd_buf->data, reply_len);
221 * ownership of the command structure as soon as it's no longer -1
223 done = cmd->done;
224 misc = cmd->misc;
226 cmd->status = rc;
228 /* Re-enable NAP mode */
229 if (smu->broken_nap)
234 spin_unlock_irqrestore(&smu->lock, flags);
269 return -ENODEV;
270 if (cmd->data_len > SMU_MAX_DATA ||
271 cmd->reply_len > SMU_MAX_DATA)
272 return -EINVAL;
274 cmd->status = 1;
275 spin_lock_irqsave(&smu->lock, flags);
276 list_add_tail(&cmd->link, &smu->cmd_list);
277 if (smu->cmd_cur == NULL)
279 spin_unlock_irqrestore(&smu->lock, flags);
282 if (!smu_irq_inited || !smu->db_irq)
295 struct smu_cmd *cmd = &scmd->cmd;
299 if (data_len > sizeof(scmd->buffer))
300 return -EINVAL;
303 cmd->cmd = command;
304 cmd->data_len = data_len;
305 cmd->data_buf = scmd->buffer;
306 cmd->reply_len = sizeof(scmd->buffer);
307 cmd->reply_buf = scmd->buffer;
308 cmd->done = done;
309 cmd->misc = misc;
313 scmd->buffer[i] = (u8)va_arg(list, int);
328 gpio = pmac_do_feature_call(PMAC_FTR_READ_GPIO, NULL, smu->doorbell);
330 smu_db_intr(smu->db_irq, smu);
346 while(cmd->status == 1)
368 cmd_buf->cmd = 0x8e;
369 cmd_buf->length = 8;
370 cmd_buf->data[0] = 0x80;
371 cmd_buf->data[1] = hex2bcd(time->tm_sec);
372 cmd_buf->data[2] = hex2bcd(time->tm_min);
373 cmd_buf->data[3] = hex2bcd(time->tm_hour);
374 cmd_buf->data[4] = time->tm_wday;
375 cmd_buf->data[5] = hex2bcd(time->tm_mday);
376 cmd_buf->data[6] = hex2bcd(time->tm_mon) + 1;
377 cmd_buf->data[7] = hex2bcd(time->tm_year - 100);
387 return -ENODEV;
396 time->tm_sec = bcd2hex(cmd.buffer[0]);
397 time->tm_min = bcd2hex(cmd.buffer[1]);
398 time->tm_hour = bcd2hex(cmd.buffer[2]);
399 time->tm_wday = bcd2hex(cmd.buffer[3]);
400 time->tm_mday = bcd2hex(cmd.buffer[4]);
401 time->tm_mon = bcd2hex(cmd.buffer[5]) - 1;
402 time->tm_year = bcd2hex(cmd.buffer[6]) + 100;
414 return -ENODEV;
418 hex2bcd(time->tm_sec),
419 hex2bcd(time->tm_min),
420 hex2bcd(time->tm_hour),
421 time->tm_wday,
422 hex2bcd(time->tm_mday),
423 hex2bcd(time->tm_mon) + 1,
424 hex2bcd(time->tm_year - 100));
480 return -ENODEV;
491 ret = -EINVAL;
496 spin_lock_init(&smu->lock);
497 INIT_LIST_HEAD(&smu->cmd_list);
498 INIT_LIST_HEAD(&smu->cmd_i2c_list);
499 smu->of_node = np;
500 smu->db_irq = 0;
501 smu->msg_irq = 0;
506 smu->cmd_buf_abs = (u32)smu_cmdbuf_abs;
507 smu->cmd_buf = __va(smu_cmdbuf_abs);
509 smu->db_node = of_find_node_by_name(NULL, "smu-doorbell");
510 if (smu->db_node == NULL) {
512 ret = -ENXIO;
515 if (of_property_read_reg(smu->db_node, 0, &data, NULL)) {
517 ret = -ENXIO;
523 * in the device-tree though.
525 smu->doorbell = data;
526 if (smu->doorbell < 0x50)
527 smu->doorbell += 0x50;
529 /* Now look for the smu-interrupt GPIO */
531 smu->msg_node = of_find_node_by_name(NULL, "smu-interrupt");
532 if (smu->msg_node == NULL)
534 if (of_property_read_reg(smu->msg_node, 0, &data, NULL)) {
535 of_node_put(smu->msg_node);
536 smu->msg_node = NULL;
539 smu->msg = data;
540 if (smu->msg < 0x50)
541 smu->msg += 0x50;
544 /* Doorbell buffer is currently hard-coded, I didn't find a proper
545 * device-tree entry giving the address. Best would probably to use
548 smu->db_buf = ioremap(0x8000860c, 0x1000);
549 if (smu->db_buf == NULL) {
551 ret = -ENXIO;
556 smu->broken_nap = pmac_get_uninorth_variant() < 4;
557 if (smu->broken_nap)
564 of_node_put(smu->msg_node);
566 of_node_put(smu->db_node);
581 timer_setup(&smu->i2c_timer, smu_i2c_retry, 0);
583 if (smu->db_node) {
584 smu->db_irq = irq_of_parse_and_map(smu->db_node, 0);
585 if (!smu->db_irq)
587 smu->db_node);
589 if (smu->msg_node) {
590 smu->msg_irq = irq_of_parse_and_map(smu->msg_node, 0);
591 if (!smu->msg_irq)
593 smu->msg_node);
600 if (smu->db_irq) {
601 if (request_irq(smu->db_irq, smu_db_intr,
605 smu->db_irq);
606 smu->db_irq = 0;
610 if (smu->msg_irq) {
611 if (request_irq(smu->msg_irq, smu_msg_intr,
615 smu->msg_irq);
616 smu->msg_irq = 0;
623 /* This has to be before arch_initcall as the low i2c stuff relies on the
636 for_each_child_of_node(smu->of_node, np)
637 if (of_device_is_compatible(np, "smu-sensors"))
638 of_platform_device_create(np, "smu-sensors",
639 &smu->of_dev->dev);
647 return -ENODEV;
648 smu->of_dev = dev;
651 * Ok, we are matched, now expose all i2c busses. We have to defer
681 * new chipsets, but that will come back and bite us
693 return smu->of_dev;
699 * i2c interface
704 void (*done)(struct smu_i2c_cmd *cmd, void *misc) = cmd->done;
705 void *misc = cmd->misc;
709 if (!fail && cmd->read) {
710 if (cmd->pdata[0] < 1)
713 memcpy(cmd->info.data, &cmd->pdata[1],
714 cmd->info.datalen);
719 /* Update status and mark no pending i2c command with lock
721 * pending next i2c command
723 spin_lock_irqsave(&smu->lock, flags);
724 smu->cmd_i2c_cur = NULL;
726 cmd->status = fail ? -EIO : 0;
728 /* Is there another i2c command waiting ? */
729 if (!list_empty(&smu->cmd_i2c_list)) {
733 newcmd = list_entry(smu->cmd_i2c_list.next,
735 smu->cmd_i2c_cur = newcmd;
736 list_del(&cmd->link);
739 list_add_tail(&cmd->scmd.link, &smu->cmd_list);
740 if (smu->cmd_cur == NULL)
743 spin_unlock_irqrestore(&smu->lock, flags);
754 struct smu_i2c_cmd *cmd = smu->cmd_i2c_cur;
756 DPRINTK("SMU: i2c failure, requeuing...\n");
759 cmd->pdata[0] = 0xff;
760 cmd->scmd.reply_len = sizeof(cmd->pdata);
761 smu_queue_cmd(&cmd->scmd);
770 DPRINTK("SMU: i2c compl. stage=%d status=%x pdata[0]=%x rlen: %x\n",
771 cmd->stage, scmd->status, cmd->pdata[0], scmd->reply_len);
774 if (scmd->status < 0)
776 else if (cmd->read) {
777 if (cmd->stage == 0)
778 fail = cmd->pdata[0] != 0;
780 fail = cmd->pdata[0] >= 0x80;
782 fail = cmd->pdata[0] != 0;
787 if (fail && --cmd->retries > 0) {
788 DPRINTK("SMU: i2c failure, starting timer...\n");
789 BUG_ON(cmd != smu->cmd_i2c_cur);
795 mod_timer(&smu->i2c_timer, jiffies + msecs_to_jiffies(5));
800 if (fail || cmd->stage != 0) {
808 scmd->reply_buf = cmd->pdata;
809 scmd->reply_len = sizeof(cmd->pdata);
810 scmd->data_buf = cmd->pdata;
811 scmd->data_len = 1;
812 cmd->pdata[0] = 0;
813 cmd->stage = 1;
814 cmd->retries = 20;
824 return -ENODEV;
827 cmd->scmd.cmd = SMU_CMD_I2C_COMMAND;
828 cmd->scmd.done = smu_i2c_low_completion;
829 cmd->scmd.misc = cmd;
830 cmd->scmd.reply_buf = cmd->pdata;
831 cmd->scmd.reply_len = sizeof(cmd->pdata);
832 cmd->scmd.data_buf = (u8 *)(char *)&cmd->info;
833 cmd->scmd.status = 1;
834 cmd->stage = 0;
835 cmd->pdata[0] = 0xff;
836 cmd->retries = 20;
837 cmd->status = 1;
839 /* Check transfer type, sanitize some "info" fields
840 * based on transfer type and do more checking
842 cmd->info.caddr = cmd->info.devaddr;
843 cmd->read = cmd->info.devaddr & 0x01;
844 switch(cmd->info.type) {
846 cmd->info.sublen = 0;
847 memset(cmd->info.subaddr, 0, sizeof(cmd->info.subaddr));
850 cmd->info.devaddr &= 0xfe;
853 if (cmd->info.sublen > 3)
854 return -EINVAL;
857 return -EINVAL;
860 /* Finish setting up command based on transfer direction
862 if (cmd->read) {
863 if (cmd->info.datalen > SMU_I2C_READ_MAX)
864 return -EINVAL;
865 memset(cmd->info.data, 0xff, cmd->info.datalen);
866 cmd->scmd.data_len = 9;
868 if (cmd->info.datalen > SMU_I2C_WRITE_MAX)
869 return -EINVAL;
870 cmd->scmd.data_len = 9 + cmd->info.datalen;
873 DPRINTK("SMU: i2c enqueuing command\n");
875 cmd->read ? "read" : "write", cmd->info.datalen,
876 cmd->info.bus, cmd->info.caddr,
877 cmd->info.subaddr[0], cmd->info.type);
880 /* Enqueue command in i2c list, and if empty, enqueue also in
883 spin_lock_irqsave(&smu->lock, flags);
884 if (smu->cmd_i2c_cur == NULL) {
885 smu->cmd_i2c_cur = cmd;
886 list_add_tail(&cmd->scmd.link, &smu->cmd_list);
887 if (smu->cmd_cur == NULL)
890 list_add_tail(&cmd->link, &smu->cmd_i2c_list);
891 spin_unlock_irqrestore(&smu->lock, flags);
938 return -EIO;
940 len -= clen;
956 DPRINTK("SMU: Query partition infos ... (irq=%d)\n", smu->db_irq);
972 * for "sdb-partition-XX" that we append at the end of the buffer
980 prop->name = ((char *)prop) + tlen - 18;
981 sprintf(prop->name, "sdb-partition-%02x", id);
982 prop->length = len;
983 prop->value = hdr;
984 prop->next = NULL;
994 if (hdr->id != id) {
996 "%02x !\n", id, hdr->id);
999 if (of_add_property(smu->of_node, prop)) {
1000 printk(KERN_DEBUG "SMU: Failed creating sdb-partition-%02x "
1023 sprintf(pname, "sdb-partition-%02x", id);
1035 part = of_get_property(smu->of_node, pname, size);
1040 *size = part->len << 2;
1086 return -ENOMEM;
1087 spin_lock_init(&pp->lock);
1088 pp->mode = smu_file_commands;
1089 init_waitqueue_head(&pp->wait);
1093 list_add(&pp->list, &smu_clist);
1095 file->private_data = pp;
1106 wake_up_all(&pp->wait);
1113 struct smu_private *pp = file->private_data;
1118 if (pp->busy)
1119 return -EBUSY;
1121 return -EFAULT;
1123 pp->mode = smu_file_events;
1129 return -EINVAL;
1134 return -EINVAL;
1135 else if (pp->mode != smu_file_commands)
1136 return -EBADFD;
1138 return -EINVAL;
1140 spin_lock_irqsave(&pp->lock, flags);
1141 if (pp->busy) {
1142 spin_unlock_irqrestore(&pp->lock, flags);
1143 return -EBUSY;
1145 pp->busy = 1;
1146 pp->cmd.status = 1;
1147 spin_unlock_irqrestore(&pp->lock, flags);
1149 if (copy_from_user(pp->buffer, buf + sizeof(hdr), hdr.data_len)) {
1150 pp->busy = 0;
1151 return -EFAULT;
1154 pp->cmd.cmd = hdr.cmd;
1155 pp->cmd.data_len = hdr.data_len;
1156 pp->cmd.reply_len = SMU_MAX_DATA;
1157 pp->cmd.data_buf = pp->buffer;
1158 pp->cmd.reply_buf = pp->buffer;
1159 pp->cmd.done = smu_user_cmd_done;
1160 pp->cmd.misc = pp;
1161 rc = smu_queue_cmd(&pp->cmd);
1176 if (!pp->busy)
1179 return -EOVERFLOW;
1180 spin_lock_irqsave(&pp->lock, flags);
1181 if (pp->cmd.status == 1) {
1182 if (file->f_flags & O_NONBLOCK) {
1183 spin_unlock_irqrestore(&pp->lock, flags);
1184 return -EAGAIN;
1186 add_wait_queue(&pp->wait, &wait);
1190 if (pp->cmd.status != 1)
1192 rc = -ERESTARTSYS;
1195 spin_unlock_irqrestore(&pp->lock, flags);
1197 spin_lock_irqsave(&pp->lock, flags);
1200 remove_wait_queue(&pp->wait, &wait);
1202 spin_unlock_irqrestore(&pp->lock, flags);
1205 if (pp->cmd.status != 0)
1206 pp->cmd.reply_len = 0;
1207 size = sizeof(hdr) + pp->cmd.reply_len;
1211 hdr.status = pp->cmd.status;
1212 hdr.reply_len = pp->cmd.reply_len;
1214 return -EFAULT;
1215 size -= sizeof(hdr);
1216 if (size && copy_to_user(buf + sizeof(hdr), pp->buffer, size))
1217 return -EFAULT;
1218 pp->busy = 0;
1236 struct smu_private *pp = file->private_data;
1238 if (pp->mode == smu_file_commands)
1240 if (pp->mode == smu_file_events)
1243 return -EBADFD;
1248 struct smu_private *pp = file->private_data;
1255 if (pp->mode == smu_file_commands) {
1256 poll_wait(file, &pp->wait, wait);
1258 spin_lock_irqsave(&pp->lock, flags);
1259 if (pp->busy && pp->cmd.status != 1)
1261 spin_unlock_irqrestore(&pp->lock, flags);
1263 if (pp->mode == smu_file_events) {
1271 struct smu_private *pp = file->private_data;
1278 file->private_data = NULL;
1281 spin_lock_irqsave(&pp->lock, flags);
1282 pp->mode = smu_file_closing;
1283 busy = pp->busy;
1286 if (busy && pp->cmd.status == 1) {
1289 add_wait_queue(&pp->wait, &wait);
1292 if (pp->cmd.status != 1)
1294 spin_unlock_irqrestore(&pp->lock, flags);
1296 spin_lock_irqsave(&pp->lock, flags);
1299 remove_wait_queue(&pp->wait, &wait);
1301 spin_unlock_irqrestore(&pp->lock, flags);
1304 list_del(&pp->list);
1327 return -ENODEV;
1329 printk(KERN_ERR "via-pmu: cannot register misc device.\n");