From patchwork Sun Jan 26 21:50:25 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?QW50dGkgU2VwcMOkbMOk?= X-Patchwork-Id: 3540151 Return-Path: X-Original-To: patchwork-linux-media@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id AAC7AC02DC for ; Sun, 26 Jan 2014 21:51:20 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id EEBC02011D for ; Sun, 26 Jan 2014 21:51:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id DA8ED20121 for ; Sun, 26 Jan 2014 21:51:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753472AbaAZVvM (ORCPT ); Sun, 26 Jan 2014 16:51:12 -0500 Received: from mail-la0-f45.google.com ([209.85.215.45]:59667 "EHLO mail-la0-f45.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753125AbaAZVvL (ORCPT ); Sun, 26 Jan 2014 16:51:11 -0500 Received: by mail-la0-f45.google.com with SMTP id b8so3899039lan.4 for ; Sun, 26 Jan 2014 13:51:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-type:content-transfer-encoding; bh=AZNQOGBEHrLH3zUxJStR91km5Llsx7T4jU5xrse6ESA=; b=tYzNFWilb0EAGrPtueT3Ax3TRhwNeLiCtDAgRDnANFWJN1WTqKRyI5E+UACmQR/+en 9bK59huXTJUI7ngLkMoPac4T1vHzSXbFOohMa2qD601E+pHjTdhzOarTY8RbRLGulnLR ugqgoseinfy+KdkAxcffJVEV0w+fhkhoT1VRuBf2hYItC5vJBtDSjLuE2ECzWoxgcPwl SmSBALOCKZXHlmKAwnmKWqNJZvNsc39BjxqsaZOfBLV4JT3oVdImoUmseWtFIZ4xbKOU rxgXUO4+W5jpFcT5JPxV+ZFfc819L+FuFuUXMGqIvVsawcBVhhrg2GFwyMxXKmKPGy0R V6Hw== X-Received: by 10.152.164.166 with SMTP id yr6mr15623595lab.1.1390773069844; Sun, 26 Jan 2014 13:51:09 -0800 (PST) Received: from pixie.elisa-laajakaista.fi (a88-114-92-24.elisa-laajakaista.fi. [88.114.92.24]) by mx.google.com with ESMTPSA id h7sm9767468lbj.1.2014.01.26.13.51.08 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 26 Jan 2014 13:51:09 -0800 (PST) From: =?UTF-8?q?Antti=20Sepp=C3=A4l=C3=A4?= To: linux-media@vger.kernel.org Cc: Mauro Carvalho Chehab , Sean Young , =?UTF-8?q?Antti=20Sepp=C3=A4l=C3=A4?= Subject: [RFCv2 PATCH 4/5] nuvoton-cir: Add support for reading/writing wakeup samples via sysfs Date: Sun, 26 Jan 2014 23:50:25 +0200 Message-Id: <1390773026-567-5-git-send-email-a.seppala@gmail.com> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: <1390773026-567-1-git-send-email-a.seppala@gmail.com> References: <1390773026-567-1-git-send-email-a.seppala@gmail.com> MIME-Version: 1.0 Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This patch adds support for reading/writing wakeup samples via sysfs to nuvoton-cir hardware. The sysfs interface for nuvoton-cir contains raw IR pulse/space lengths. If value is negative it is a space, otherwise it is a pulse. Signed-off-by: Antti Seppälä --- drivers/media/rc/nuvoton-cir.c | 118 +++++++++++++++++++++++++++++++++++++++++ drivers/media/rc/nuvoton-cir.h | 2 + 2 files changed, 120 insertions(+) diff --git a/drivers/media/rc/nuvoton-cir.c b/drivers/media/rc/nuvoton-cir.c index 21ee0dc..015f3a8 100644 --- a/drivers/media/rc/nuvoton-cir.c +++ b/drivers/media/rc/nuvoton-cir.c @@ -531,6 +531,121 @@ static int nvt_set_tx_carrier(struct rc_dev *dev, u32 carrier) return 0; } +static int nvt_validate_wakeup_codes(struct list_head *wakeup_code_list) +{ + int i = 0; + const int MAX_SAMPLE = US_TO_NS(BUF_LEN_MASK * SAMPLE_PERIOD); + struct rc_wakeup_code *code; + list_for_each_entry(code, wakeup_code_list, list_item) { + + /* Prevent writing too many or invalid codes */ + if (++i > WAKE_FIFO_LEN) + return -EINVAL; + if (abs(code->value) > MAX_SAMPLE) + return -EINVAL; + } + + return i; +} + +static int nvt_wakeup_codes(struct rc_dev *dev, + struct list_head *wakeup_code_list, int write) +{ + int i = 0; + u8 cnt, val, reg, reg_learn_mode; + unsigned long flags; + struct rc_wakeup_code *code; + struct nvt_dev *nvt = dev->priv; + + nvt_dbg_wake("%sing wakeup codes", write ? "writ" : "read"); + + if (write) { + + /* Validate wake codes (count and values) */ + i = nvt_validate_wakeup_codes(wakeup_code_list); + if (i < 0) + return -EINVAL; + + reg = nvt_cir_wake_reg_read(nvt, CIR_WAKE_IRCON); + reg_learn_mode = reg & ~CIR_WAKE_IRCON_MODE0; + reg_learn_mode |= CIR_WAKE_IRCON_MODE1; + + /* Lock the learn area to prevent racing with wake-isr */ + spin_lock_irqsave(&nvt->nvt_lock, flags); + + /* Enable fifo writes */ + nvt_cir_wake_reg_write(nvt, reg_learn_mode, CIR_WAKE_IRCON); + + /* Clear cir wake rx fifo */ + nvt_clear_cir_wake_fifo(nvt); + + pr_info("Wake samples (%d) =", i); + + /* Write wake samples to fifo */ + list_for_each_entry(code, wakeup_code_list, list_item) { + + /* Convert to driver format */ + val = DIV_ROUND_UP(abs(code->value), 1000L) + / SAMPLE_PERIOD; + + if (code->value > 0) + val |= BUF_PULSE_BIT; + + pr_cont(" %02x", val); + nvt_cir_wake_reg_write(nvt, val, + CIR_WAKE_WR_FIFO_DATA); + } + pr_cont("\n"); + + /* Switch cir to wakeup mode and disable fifo writing */ + nvt_cir_wake_reg_write(nvt, reg, CIR_WAKE_IRCON); + + /* Set number of bytes needed for wake */ + nvt_cir_wake_reg_write(nvt, i ? i : + CIR_WAKE_FIFO_CMP_BYTES, + CIR_WAKE_FIFO_CMP_DEEP); + + spin_unlock_irqrestore(&nvt->nvt_lock, flags); + + } else { + + /* Scroll to index 0 */ + while (nvt_cir_wake_reg_read(nvt, CIR_WAKE_RD_FIFO_ONLY_IDX)) { + nvt_cir_wake_reg_read(nvt, CIR_WAKE_RD_FIFO_ONLY); + + /* Stuck index guardian */ + if (++i > 255) { + nvt_dbg_wake("Idx reg is stuck!"); + break; + } + } + + /* Get size of wake fifo and allocate memory for the bytes */ + cnt = nvt_cir_wake_reg_read(nvt, CIR_WAKE_FIFO_COUNT); + + for (i = 0; i < cnt; i++) { + code = kmalloc(sizeof(struct rc_wakeup_code), + GFP_KERNEL | GFP_ATOMIC); + if (!code) + return -ENOMEM; + + val = nvt_cir_wake_reg_read(nvt, + CIR_WAKE_RD_FIFO_ONLY); + + /* Convert to human readable sample-pulse format */ + code->value = US_TO_NS((val & BUF_LEN_MASK) + * SAMPLE_PERIOD); + if (!(val & BUF_PULSE_BIT)) + code->value *= -1; + + list_add_tail(&code->list_item, wakeup_code_list); + } + + return cnt; + } + + return 0; +} /* * nvt_tx_ir * @@ -1043,10 +1158,13 @@ static int nvt_probe(struct pnp_dev *pdev, const struct pnp_device_id *dev_id) rdev->priv = nvt; rdev->driver_type = RC_DRIVER_IR_RAW; rdev->allowed_protos = RC_BIT_ALL; + rdev->allowed_wake_protos = RC_BIT_OTHER; + rdev->enabled_wake_protos = RC_BIT_OTHER; rdev->open = nvt_open; rdev->close = nvt_close; rdev->tx_ir = nvt_tx_ir; rdev->s_tx_carrier = nvt_set_tx_carrier; + rdev->s_wakeup_codes = nvt_wakeup_codes; rdev->input_name = "Nuvoton w836x7hg Infrared Remote Transceiver"; rdev->input_phys = "nuvoton/cir0"; rdev->input_id.bustype = BUS_HOST; diff --git a/drivers/media/rc/nuvoton-cir.h b/drivers/media/rc/nuvoton-cir.h index 07e8310..24ff630 100644 --- a/drivers/media/rc/nuvoton-cir.h +++ b/drivers/media/rc/nuvoton-cir.h @@ -64,6 +64,8 @@ static int debug; #define TX_BUF_LEN 256 #define RX_BUF_LEN 32 +#define WAKE_FIFO_LEN 67 + struct nvt_dev { struct pnp_dev *pdev; struct rc_dev *rdev;