I did 300 000 insmod/rmmod cycles and all was ok, but the memory 
consumption increased from 23MB to 36MB. I think, it is because of timers. 
As Hans de Goede said, it is better to use devm_clk_get to get the clock. I 
use this function in my first patch, but I not found a way to get apb0_ir0 
clock, because it is not registered. I forced registering with this part of 
first patch:

diff --git a/drivers/clk/sunxi/clk-sunxi.c b/drivers/clk/sunxi/clk-sunxi.c
index 3be8846..83a20fa 100644
--- a/drivers/clk/sunxi/clk-sunxi.c
+++ b/drivers/clk/sunxi/clk-sunxi.c
@@ -1036,11 +1036,17 @@ static void __init sunxi_gates_clk_setup(struct 
device_node *node,
  /* No driver claims this clock, but it should remain gated */
  ignore = !strcmp("ahb_sdram", clk_name) ? CLK_IGNORE_UNUSED : 0;
 
+
  clk_data->clks[i] = clk_register_gate(NULL, clk_name,
        clk_parent, ignore,
        reg + 4 * (i/32), i % 32,
        0, &clk_lock);
  WARN_ON(IS_ERR(clk_data->clks[i]));
+ 
+ if(!IS_ERR(clk_data->clks[i])) {
+ clk_register_clkdev(clk_data->clks[i], clk_name, NULL);
+ }
+

I am looking for sane way to do same without 
patching drivers/clk/sunxi/clk-sunxi.c.

Also I've tested parallel module load/unload. I executed folowwing commands:
while true; do echo bay; rmmod sunxi-ir; insmod /sunxi-ir.ko; done &
while true; do echo bay; rmmod sunxi-ir; insmod /sunxi-ir.ko; done &
... 20 times ...

It runs ok, but when I press buttons on my IR remote controller in the 
process, I got a kernel OOPS(null pointer dereference) sometimes. I am 
fixing this.

Best,
Alexander Bersenev, Institute of Mathematics and Mechanics, Russia
понедельник, 28 апреля 2014 г., 2:11:19 UTC+6 пользователь Александр 
Берсенев написал:
>
> Hello,
>
> Thank you for your replies. I decided to use Wingrime's patch as a base 
> for my patch to not doing the same work twice. I've changed code a bit, 
> espectially in initialization and finalisation part. Now it not freezes a 
> system after insmod/rmmod/insmod.
>
> This patch applies on Hans de Goede's sunxi-devel branch(3.15-rc2). It 
> works only on cubietruck, but it seems that only changes in *.dts and 
> *.idts files are required to add the new platform.
>
> Here is the patch:
> Signed-off-by: Alexander Bersenev <b...@hackerdom.ru>
>
> diff --git a/Documentation/devicetree/bindings/media/sunxi-ir.txt 
> b/Documentation/devicetree/bindings/media/sunxi-ir.txt
> new file mode 100644
> index 0000000..8181efc
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/media/sunxi-ir.txt
> @@ -0,0 +1,21 @@
> +Device-Tree bindings for SUNXI IR controller found in sunXi SoC family
> +
> +Required properties:
> +       - compatible: Should be "allwinner,sunxi-ir".
> +       - clocks: First clock should constain SoC gate for IR clock
> +       second should constain IR feed clock itself.
> +       - interrupts: Should constain IR IRQ number.
> +       -reg: Should constain IO map address for IR.
> +
> +Optional properties:
> +       - linux,rc-map-name: Remote control map name.
> +
> +Example:
> +
> +       ir0: ir@01c21800 {
> +            compatible = "allwinner,sunxi-ir";
> +            clocks = <&apb0_gates 6>, <&ir0_clk>;
> +            interrupts = <0 5 1>;
> +            reg = <0x01C21800 0x40>;
> +            linux,rc-map-name = "rc-rc6-mce";
> +       };
> diff --git a/arch/arm/boot/dts/sun7i-a20-cubietruck.dts 
> b/arch/arm/boot/dts/sun7i-a20-cubietruck.dts
> index e288562..294c115 100644
> --- a/arch/arm/boot/dts/sun7i-a20-cubietruck.dts
> +++ b/arch/arm/boot/dts/sun7i-a20-cubietruck.dts
> @@ -121,6 +121,13 @@
>   };
>   };
>  
> + ir0: ir@01c21800 {
> + pinctrl-names = "default";
> + pinctrl-0 = <&ir0_pins_a>;
> + gpios = <&pio 1 4 0>;
> + status = "okay";
> + };
> +
>   uart0: serial@01c28000 {
>   pinctrl-names = "default";
>   pinctrl-0 = <&uart0_pins_a>;
> diff --git a/arch/arm/boot/dts/sun7i-a20.dtsi 
> b/arch/arm/boot/dts/sun7i-a20.dtsi
> index 0ae2b77..4597731 100644
> --- a/arch/arm/boot/dts/sun7i-a20.dtsi
> +++ b/arch/arm/boot/dts/sun7i-a20.dtsi
> @@ -724,6 +724,19 @@
>   allwinner,drive = <2>;
>   allwinner,pull = <0>;
>   };
> +
> + ir0_pins_a: ir0@0 {
> +    allwinner,pins = "PB3","PB4";
> +    allwinner,function = "ir0";
> +    allwinner,drive = <0>;
> +    allwinner,pull = <0>;
> + };
> + ir1_pins_a: ir1@0 {
> +    allwinner,pins = "PB22","PB23";
> +    allwinner,function = "ir1";
> +    allwinner,drive = <0>;
> +    allwinner,pull = <0>;
> + };
>   };
>  
>   timer@01c20c00 {
> @@ -937,5 +950,21 @@
>   #interrupt-cells = <3>;
>   interrupts = <1 9 0xf04>;
>   };
> +
> +       ir0: ir@01c21800 {
> +     compatible = "allwinner,sunxi-ir";
> + clocks = <&apb0_gates 6>, <&ir0_clk>;
> + interrupts = <0 5 4>;
> + reg = <0x01C21800 0x40>;
> + status = "disabled";
> + };
> +
> +       ir1: ir@01c21c00 {
> +     compatible = "allwinner,sunxi-ir";
> + clocks = <&apb0_gates 7>, <&ir1_clk>;
> + interrupts = <0 6 4>;
> + reg = <0x01C21c00 0x40>;
> + status = "disabled";
> + };
>   };
>  };
> diff --git a/drivers/media/rc/Kconfig b/drivers/media/rc/Kconfig
> index 8fbd377..9427fad 100644
> --- a/drivers/media/rc/Kconfig
> +++ b/drivers/media/rc/Kconfig
> @@ -343,4 +343,14 @@ config RC_ST
>  
>   If you're not sure, select N here.
>  
> +config IR_SUNXI
> +    tristate "SUNXI IR remote control"
> +    depends on RC_CORE
> +    depends on ARCH_SUNXI
> +    ---help---
> +      Say Y if you want to use sunXi internal IR Controller
> +
> +      To compile this driver as a module, choose M here: the module will
> +      be called sunxi-ir.
> +
>  endif #RC_DEVICES
> diff --git a/drivers/media/rc/Makefile b/drivers/media/rc/Makefile
> index f8b54ff..93cdbe9 100644
> --- a/drivers/media/rc/Makefile
> +++ b/drivers/media/rc/Makefile
> @@ -32,4 +32,5 @@ obj-$(CONFIG_IR_GPIO_CIR) += gpio-ir-recv.o
>  obj-$(CONFIG_IR_IGUANA) += iguanair.o
>  obj-$(CONFIG_IR_TTUSBIR) += ttusbir.o
>  obj-$(CONFIG_RC_ST) += st_rc.o
> +obj-$(CONFIG_IR_SUNXI) += sunxi-ir.o
>  obj-$(CONFIG_IR_IMG) += img-ir/
> diff --git a/drivers/media/rc/sunxi-ir.c b/drivers/media/rc/sunxi-ir.c
> new file mode 100644
> index 0000000..e9622f9
> --- /dev/null
> +++ b/drivers/media/rc/sunxi-ir.c
> @@ -0,0 +1,308 @@
> +/*
> + * Driver for Allwinner sunXi IR controller
> + *
> + * Copyright (C) 2014 Alexsey Shestacov <wingr...@linux-sunxi.org>
> + *
> + * Based on sun5i-ir.c:
> + * Copyright (C) 2007-2012 Daniel Wang
> + * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
> + *
> + * This program is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU General Public License as
> + * published by the Free Software Foundation; either version 2 of
> + * the License, or (at your option) any later version.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> + * GNU General Public License for more details.
> + */
> +
> +#include <linux/module.h>
> +#include <linux/clk.h>
> +#include <linux/delay.h>
> +#include <linux/module.h>
> +#include <linux/init.h>
> +#include <linux/input.h>
> +#include <linux/interrupt.h>
> +#include <linux/ioport.h>
> +#include <linux/irq.h>
> +#include <linux/io.h>
> +#include <linux/keyboard.h>
> +#include <linux/slab.h>
> +#include <linux/timer.h>
> +#include <linux/of_irq.h>
> +#include <linux/of_address.h>
> +#include <linux/of_device.h>
> +#include <linux/of_platform.h>
> +#include <media/rc-core.h>
> +
> +#define SUNXI_IR_DEV "sunxi-ir"
> +
> +/* Registers */
> +/* IR Control */
> +#define SUNXI_IR_CTRL_REG      0x00
> +/* Rx Config */
> +#define SUNXI_IR_RXCFG_REG      0x10
> +/* Rx Data */
> +#define SUNXI_IR_RXDAT_REG     0x20
> +/* Rx Interrupt Enable */
> +#define SUNXI_IR_RXINTE_REG     0x2C
> +/* Rx Interrupt Status */
> +#define SUNXI_IR_RXINTS_REG    0x30
> +/* IR Sample Config */
> +#define SUNXI_IR_SPLCFG_REG    0x34
> +
> +/* Bit Definition of IR_RXINTS_REG Register */
> +#define SUNXI_IR_RXINTS_RXOF   (0x1<<0) /* Rx FIFO Overflow */
> +#define SUNXI_IR_RXINTS_RXPE   (0x1<<1) /* Rx Packet End */
> +#define SUNXI_IR_RXINTS_RXDA   (0x1<<4) /* Rx FIFO Data Available */
> +/*Hardware supported fifo size*/
> +#define SUNXI_IR_FIFO_SIZE  16
> +/*How much messages in fifo triggers IRQ */
> +#define SUNXI_IR_FIFO_TRIG 8
> +/* Required frequency for IR0 or IR1 clock in CIR mode*/
> +#define SUNXI_IR_BASE_CLK 8000000
> +/* Frequency after IR internal divider  */
> +#define SUNXI_IR_CLK  (SUNXI_IR_BASE_CLK / 64)
> +/* Sample period in ns */
> +#define SUNXI_IR_SAMPLE (1000000000ul / SUNXI_IR_CLK)
> +/* Filter threshold in samples  */
> +#define SUNXI_IR_RXFILT                1
> +/* Idle Threshold */
> +#define SUNXI_IR_RXIDLE               20
> +#define SUNXI_IR_TIMEOUT      120
> +
> +struct sunxi_ir {
> + spinlock_t      ir_lock;
> + struct rc_dev   *rc;
> + void __iomem    *base;
> + int             irq;
> + struct clk      *clk;
> + struct clk      *apb_clk;
> + const char      *map_name;
> +};
> +
> +static irqreturn_t sunxi_ir_irq(int irqno, void *dev_id)
> +{
> + unsigned long status;
> + unsigned char dt;
> + unsigned int cnt, rc;
> + struct sunxi_ir *ir = dev_id;
> + DEFINE_IR_RAW_EVENT(rawir);
> + status = readl(ir->base + SUNXI_IR_RXINTS_REG);
> +
> + /* clean all pending statuses */
> + writel(status | 0xff, ir->base + SUNXI_IR_RXINTS_REG);
> +
> + if (status & SUNXI_IR_RXINTS_RXDA) {
> + /* How much messages in fifo */
> + rc  = ((status>>8) & 0x3f);
> + /* Sanity check */
> + rc = rc > SUNXI_IR_FIFO_SIZE ? SUNXI_IR_FIFO_SIZE : rc;
> + /* if we have data */
> + for (cnt = 0; cnt < rc; cnt++) {
> + /* for each bit in fifo */
> + dt = (unsigned char)readl(ir->base +
> +  SUNXI_IR_RXDAT_REG);
> + rawir.pulse = (dt & 0x80) != 0;
> + rawir.duration = (dt & 0x7f)*SUNXI_IR_SAMPLE;
> + ir_raw_event_store_with_filter(ir->rc, &rawir);
> + }
> + }
> +
> + if (status & SUNXI_IR_RXINTS_RXOF)
> + ir_raw_event_reset(ir->rc);
> + else if (status & SUNXI_IR_RXINTS_RXPE)
> + ir_raw_event_handle(ir->rc);
> +
> + return IRQ_HANDLED;
> +}
> +
> +
> +static int sunxi_ir_probe(struct platform_device *pdev)
> +{
> + int ret = 0;
> +
> + unsigned long tmp = 0;
> +
> + struct device *dev = &pdev->dev;
> + struct device_node *dn = dev->of_node;
> + struct resource *res;
> + struct sunxi_ir *ir;
> +
> + ir = devm_kzalloc(dev, sizeof(struct sunxi_ir), GFP_KERNEL);
> + if (!ir)
> + return -ENOMEM;
> +
> + /* Clock */
> + ir->apb_clk = of_clk_get(dn, 0);
> + if (IS_ERR(ir->apb_clk)) {
> + dev_err(dev, "failed to get a apb clock.\n");
> + return PTR_ERR(ir->clk);
> + }
> + ir->clk = of_clk_get(dn, 1);
> + if (IS_ERR(ir->clk)) {
> + dev_err(dev, "failed to get a ir clock.\n");
> + return PTR_ERR(ir->clk);
> + }
> + ret = clk_set_rate(ir->clk, SUNXI_IR_BASE_CLK);
> + if (ret) {
> + dev_err(dev, "set ir base clock failed!\n");
> + return ret;
> + }
> +
> + if (clk_prepare_enable(ir->apb_clk)) {
> + dev_err(dev, "try to enable apb_ir_clk failed\n");
> + return -EINVAL;
> + }
> +
> + if (clk_prepare_enable(ir->clk)) {
> + dev_err(dev, "try to enable ir_clk failed\n");
> + clk_disable_unprepare(ir->apb_clk);
> + return -EINVAL;
> + }
> +
> + /* IO */
> + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> +
> + ir->base = devm_ioremap_resource(dev, res);
> + if (IS_ERR(ir->base)) {
> + dev_err(dev, "failed to map registers\n");
> + ret = -ENOMEM;
> + goto exit_clkerr;
> + }
> +
> + /* IRQ */
> + ir->irq = platform_get_irq(pdev, 0);
> + if (ir->irq < 0) {
> + dev_err(dev, "no irq resource\n");
> + ret = -EINVAL;
> + goto exit_clkerr;
> + }
> +
> + ret = devm_request_irq(dev, ir->irq, sunxi_ir_irq, 0, SUNXI_IR_DEV, ir);
> + if (ret) {
> + dev_err(dev, "failed request irq\n");
> + ret = -EINVAL;
> + goto exit_clkerr;
> + }
> +
> + ir->rc = rc_allocate_device();
> +
> + if (!ir->rc) {
> + ret = -ENOMEM;
> + goto exit_clkerr;
> + }
> +
> + ir->rc->priv = ir;
> + ir->rc->input_name = SUNXI_IR_DEV;
> + ir->rc->input_phys = "sunxi-ir/input0";
> + ir->rc->input_id.bustype = BUS_HOST;
> + ir->rc->input_id.vendor = 0x0001;
> + ir->rc->input_id.product = 0x0001;
> + ir->rc->input_id.version = 0x0100;
> + ir->map_name = of_get_property(dn, "linux,rc-map-name", NULL);
> + ir->rc->map_name = ir->map_name ?: RC_MAP_EMPTY;
> + ir->rc->dev.parent = dev;
> + ir->rc->driver_type = RC_DRIVER_IR_RAW;
> + rc_set_allowed_protocols(ir->rc, RC_BIT_ALL);
> + ir->rc->rx_resolution = SUNXI_IR_SAMPLE;
> + ir->rc->timeout = MS_TO_NS(SUNXI_IR_TIMEOUT);
> + ir->rc->driver_name = SUNXI_IR_DEV;
> +
> + /* Enable CIR Mode */
> + writel(0x3<<4, ir->base+SUNXI_IR_CTRL_REG);
> +
> + /* Config IR Sample Register */
> +
> + /* Fsample = clk   */
> + tmp = 0x0<<0;
> + /* Set Filter Threshold */
> + tmp |= (SUNXI_IR_RXFILT&0x3f)<<2;
> + /* Set Idle Threshold */
> + tmp |= ((SUNXI_IR_RXIDLE)&0xff)<<8;
> + writel(tmp, ir->base + SUNXI_IR_SPLCFG_REG);
> +
> + /* Invert Input Signal */
> + writel(0x1<<2, ir->base + SUNXI_IR_RXCFG_REG);
> +
> + /* Clear All Rx Interrupt Status */
> + writel(0xff, ir->base + SUNXI_IR_RXINTS_REG);
> +
> + /* Enable IRQ on data, overflow, packed end */
> + tmp = (0x1<<4)|0x3;
> +
> + /* Rx FIFO Threshold = 4 */
> + tmp |= ((SUNXI_IR_FIFO_TRIG)-1)<<8;
> +
> + writel(tmp, ir->base + SUNXI_IR_RXINTE_REG);
> +
> + /* Enable IR Module */
> + tmp = readl(ir->base + SUNXI_IR_CTRL_REG);
> +
> + writel(tmp|0x3, ir->base + SUNXI_IR_CTRL_REG);
> +
> + ret = rc_register_device(ir->rc);
> + if (ret) {
> + dev_err(dev, "can't register rc device\n");
> + ret = -EINVAL;
> + goto exit_free_dev;
> + }
> +
> + platform_set_drvdata(pdev, ir);
> +
> + dev_info(dev, "initialized sunXi IR driver\n");
> + return 0;
> +
> +exit_free_dev:
> + rc_free_device(ir->rc);
> +exit_clkerr:
> + clk_disable_unprepare(ir->clk);
> + clk_disable_unprepare(ir->apb_clk);
> +
> + return ret;
> +}
> +
> +static int sunxi_ir_remove(struct platform_device *pdev)
> +{
> + unsigned long flags;
> + struct sunxi_ir *ir = platform_get_drvdata(pdev);
> +
> + clk_disable_unprepare(ir->clk);
> + clk_disable_unprepare(ir->apb_clk);
> +
> + spin_lock_irqsave(&ir->ir_lock, flags);
> + /* disable IR IRQ */
> + writel(0, ir->base + SUNXI_IR_RXINTE_REG);
> + /* clear All Rx Interrupt Status */
> + writel(0xff, ir->base + SUNXI_IR_RXINTS_REG);
> + /* disable IR */
> + writel(0, ir->base + SUNXI_IR_CTRL_REG);
> + spin_unlock_irqrestore(&ir->ir_lock, flags);
> +
> + rc_unregister_device(ir->rc);
> + return 0;
> +}
> +
> +static struct of_device_id sunxi_ir_match[] = {
> + { .compatible = "allwinner,sunxi-ir", },
> + {},
> +};
> +
> +static struct platform_driver sunxi_ir_driver = {
> + .probe          = sunxi_ir_probe,
> + .remove         = sunxi_ir_remove,
> + .driver = {
> + .name = SUNXI_IR_DEV,
> + .owner = THIS_MODULE,
> + .of_match_table = sunxi_ir_match,
> + },
> +};
> +
> +module_platform_driver(sunxi_ir_driver);
> +
> +
> +MODULE_DESCRIPTION("Allwinner sunXi IR controller driver");
> +MODULE_AUTHOR("Alexsey Shetacov <wingr...@linux-sunxi.org>");
> +MODULE_LICENSE("GPL");
>
>
> суббота, 26 апреля 2014 г., 22:13:13 UTC+6 пользователь Alejandro Mery 
> написал:
>>
>>
>>
>> On 26/04/14 17:11, Rudi wrote: 
>> > Hi, 
>> > 
>> >> Ah I just send a mail about the need to use the rc-core subsys myself, 
>> >> I did not 
>> >> know we already had a driver using that. Do you have a link to the old 
>> >> posting of 
>> >> that driver ? That would be a great starting point for Alexander to 
>> >> get something 
>> >> ready for the mainline (iirc the cir driver was against 3.4 ?) 
>> > 
>> > I carefully put it aside ;-). Find a patch that hooks it into 3.4.79 
>> here: 
>> > 
>> > 
>> https://github.com/OpenBricks/openbricks/blob/master/config/platforms/arm/a10/packages/linux/patches/0002-ir-core-support.diff
>>  
>>
>> can you resubmit it here as a "proper patch" for the 3.4 tree? 
>>
>> Thanks, 
>> Alejandro Mery 
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"linux-sunxi" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to linux-sunxi+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to